Home > Mobile >  POST JSON Body with CURL in PHP
POST JSON Body with CURL in PHP

Time:10-29

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"
    }
  }
}

CodePudding user response:

Try use

CURLOPT_POSTFIELDS => json_encode($data);

Reference How to POST JSON Data With PHP cURL?

CodePudding user response:

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

  • Related