0

I have running code like the following

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://chat-service.com/api/direct',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_POSTFIELDS =>'{
  "to_number": "62111111111",
  "to_name": "Mr X",
  "message_template_id": "abc123abc123",
  "channel_integration_id": "123abc123abc",
  "language": {
    "code": "id"
  },
  "parameters": {
    "body": [
      {
        "key": "1",
        "value": "name",
        "value_text": "Mr X"
      },
      {
        "key": "2",
        "value": "vehiclereg",
        "value_text": "L0001X"
      },
      {
        "key": "3",
        "value": "date",
        "value_text": "29 Feb 2022"
      }
    ]
  }
}

',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer 1122334455',
    'target_channel: trial'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
?>

now i'd like to store CURLOPT_POSTFIELDS value as variable and this is my code but stil not running

<?php

$curl = curl_init();
$data = array
    (
    "to_number"=>'62111111111',
    "to_name"=>'Mr X',
    "message_template_id"=>'abc123abc123',
    "channel_integration_id"=>'123abc123abc',
    "language"=>
    array 
        (
            "code"=>"id"
        ),
    "parameters"=>
    array
        (
            "body"=>array 
            (
                "key"=>"1",
                "value"=>"name",
                "value_text"=>"Mr X"
            ),
            "body"=>array 
            (
                "key"=>"2",
                "value"=>"vehiclereg",
                "value_text"=>"L0001X"
            ),
            "body"=>array 
            (
                "key"=>"3",
                "value"=>"date",
                "value_text"=>"29 Feb 2022"
            )
        )
    );
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer 1122334455',
    'target_channel: trial'
    ),
CURLOPT_URL => 'https://chat-service.com/api/direct',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POSTFIELDS => http_build_query($data);
));

$response = curl_exec($curl);
curl_close($curl);

echo "<pre>";
print_r($response)

?>

In my opininion this line is the suspect why the code not working but still don't know how to fix it

Running one

  "parameters": {
    "body": [
      {
        "key": "1",
        "value": "name",
        "value_text": "Mr X"
      },
      {
        "key": "2",
        "value": "vehiclereg",
        "value_text": "L0001X"
      },
      {
        "key": "3",
        "value": "date",
        "value_text": "29 Feb 2022"
      }
    ]
  }

Not Running

"parameters"=>
    array
        (
            "body"=>array 
            (
                "key"=>"1",
                "value"=>"name",
                "value_text"=>"Mr X"
            ),
            "body"=>array 
            (
                "key"=>"2",
                "value"=>"vehiclereg",
                "value_text"=>"L0001X"
            ),
            "body"=>array 
            (
                "key"=>"3",
                "value"=>"date",
                "value_text"=>"29 Feb 2022"
            )
        )

I tried googling it everywhere find some similar case like on this one send post json with php (curl) but still can fix it

i did encode like this

<?php
$data = array
    (
    "to_number"=>'62111111111',
    "to_name"=>'Mr X',
    "message_template_id"=>'abc123abc123',
    "channel_integration_id"=>'123abc123abc',
    "language"=>
        array 
        (
            "code"=>"id"
        ),
    "parameters"=>
        array
        (
            "body"=>array 
                (
                "key"=>"1",
                "value"=>"name",
                "value_text"=>"Mr X"
                ),
             "body"=>array 
                (
                "key"=>"2",
                "value"=>"vehiclereg",
                "value_text"=>"L0001X"
                ),
             "body"=>array 
                (
                "key"=>"3",
                "value"=>"date",
                "value_text"=>"29 Feb 2022"
                )
        )
    );

echo json_encode($data);
?>

and this is the result

{
  "to_number": "62111111111",
  "to_name": "Mr X",
  "message_template_id": "abc123abc123",
  "channel_integration_id": "123abc123abc",
  "language": {
    "code": "id"
  },
  "parameters": {
    "body": {
      "key": "3",
      "value": "date",
      "value_text": "29 Feb 2022"
    }
  }
}
Reza
  • 65
  • 9
  • Try using json_encode if POST Data. It could be releated to multi dimensional array. – Bhoj Raj Bhatta Oct 27 '21 at 08:53
  • The main problem in your data is that you want to use the same key for the php associative array. You using the body for 3 times. The keys(and the data) replacing each others, only the last going to stay in the array, the others going to be overwritten. https://www.php.net/manual/en/language.types.array.php – Orbán Zoltán Oct 27 '21 at 09:34
  • @OrbánZoltán but the first code work fine and here's the JSON respon from server "body": { "1": "name", "2": "vehiclereg", "3": "date" } – Reza Oct 28 '21 at 02:35
  • @BhojRajBhatta i did add json encode – Reza Oct 28 '21 at 02:36

2 Answers2

1

Try use

CURLOPT_POSTFIELDS => json_encode($data);

Reference How to POST JSON Data With PHP cURL?

DevinE
  • 149
  • 1
  • 4
  • i did but still can't fix it – Reza Oct 27 '21 at 08:57
  • your `parameters` has 3 key `body`, check again – DevinE Oct 27 '21 at 09:01
  • yes precisely that what i'd like to fix `body` suppose to has 3 value – Reza Oct 27 '21 at 09:03
  • service that i'd like to hit need 3 value from `body` which is key 1 for name key 2 for vehiclereg and key3 for date. As i said my first code is running normally – Reza Oct 27 '21 at 09:04
  • Sorry, I can not find your service document, can you send it? chat-service.com – DevinE Oct 27 '21 at 09:07
  • that's dummy data actually – Reza Oct 27 '21 at 09:08
  • you must process data from `curl` with backend and do not send data with same key in array, the previous same key in array will be replace by the last – DevinE Oct 27 '21 at 09:13
  • but the first code work fine and here's the JSON respon from server "body": { "1": "name", "2": "vehiclereg", "3": "date" } – Reza Oct 28 '21 at 02:32
  • fix your parameters code `"parameters"=>array(array("key"=>"1","value"=>"name","value_text"=>"Mr X" ), array ( "key"=>"2", "value"=>"vehiclereg", "value_text"=>"L0001X" ), array ( "key"=>"3", "value"=>"date", "value_text"=>"29 Feb 2022" ) )` – DevinE Oct 28 '21 at 07:19
  • unexpected T_Double_Arrow in "key"=>"2" – Reza Oct 28 '21 at 09:01
  • 1
    `"parameters"=>[["key"=>"1","value"=>"name","value_text"=>"Mr X"],["key"=>"2","value"=>"vehiclereg","value_text"=>"L0001X"], ["key"=>"3", "value"=>"date", "value_text"=>"29 Feb 2022"]],` use this or under answers – DevinE Oct 29 '21 at 01:32
1

Finally found the solution based on trial error and googling everywhere

<?php

$curl = curl_init();
$data = 
[
    "to_number"=>'62111111111',
    "to_name"=>'Mr X',
    "message_template_id"=>'abc123abc123',
    "channel_integration_id"=>'123abc123abc',
    "language"=> 
    [
        "code"=>"id"
    ],
    "parameters"=>
    [
        "body" =>
        [ 
            [
                "key"=>"1",
                "value"=>"name",
                "value_text"=>"Mr X"
            ],
            [
                "key"=>"2",
                "value"=>"vehiclereg",
                "value_text"=>"L0001X"
            ],
            [
                "key"=>"3",
                "value"=>"date",
                "value_text"=>"29 Februari 2022"
            ]
        ]
    ]
];
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer 1122334455',
    'target_channel: tria;'
    ),
CURLOPT_URL => 'https://chat-service.com/api/direct',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POSTFIELDS => json_encode($data)
));

$response = curl_exec($curl);
curl_close($curl);

echo "<pre>";
print_r($response);
?>

thank you stack overflow for inspiring

Reza
  • 65
  • 9
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 29 '21 at 01:18