How to Post Json Data With PHP Curl

PHP CURL Using POST Raw JSON Data

If you wanna use Content-type: application/json and raw data, seem your data should be in json format

$ch = curl_init();
$headers = [
'x-api-key: XXXXXX',
'Content-Type: text/plain'
];
$postData = [
'data1' => 'value1',
'data2' => 'value2'
];
curl_setopt($ch, CURLOPT_URL,"XXXXXX");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec ($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

POST JSON Body with CURL in PHP

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

Post json data with php cURL

Moving my comment to an answer and expanding upon it:

Running the following should give you a bit more data.

var_dump(curl_getinfo($ch));

As stated by the original poster, the issue turned out to be the cert was being marked as invalid. More often than not, this is due to the cert being a self-signed cert, but any invalid cert would throw this error.

For development this is acceptable. However, in production you ALWAYS want to verify the url you are requesting has a valid cert. This is normally for security reasons.

Adding the curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); resolved the issue.

JSON POST data with php curl

Thanks, error slove. that i share my code.

$url = 'http://username:password@demoerp.rawntech.com/demoerp/org.openbravo.service.json.jsonrest/BusinessPartner';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array(
'entityName' => 'BusinessPartner',
'id' => 'A6750F0D15334FB890C254369AC750A8',
'name' => 'khayer'
);
$data_string = json_encode(array("data" =>$jsonData));

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

curl_close($ch);

echo "$result";

Post a JSON object from PHP using cUrl

Post json objectusing curl.

$data = array('value1' => $value1, 'value2' => $value2);                                                                    
$data_string = json_encode($data);

$ch = curl_init('http://api.local/rest/users'); // where to post
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);

$result = curl_exec($ch);


Related Topics



Leave a reply



Submit