How to Post Json to PHP With 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);

How to post JSON to PHP with curl

Jordans analysis of why the $_POST-array isn't populated is correct. However, you can use

$data = file_get_contents("php://input");

to just retrieve the http body and handle it yourself. See PHP input/output streams.

From a protocol perspective this is actually more correct, since you're not really processing http multipart form data anyway. Also, use application/json as content-type when posting your request.

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

Hot to send a nested json post request with cURL and PHP

<?php

$data = [
'assistanceRequest' => [
'type' => 'TECHNICAL_DATA',
'deliveryMode' => 'NORMAL',
"creationDate" => "2020-04-09T10:09:00+02:00",
"source" => "ATD",
"language" => "FR",
"country" => "FR"
],
'customer' => [
"code" => 123456,
"login" => "client@company.com",
"companyName" => "Workshop Harris",
"phoneNumber" => "+44 123456789",
"email" => "workshop@company.com"
],
];

echo "<pre>"; print_r(json_encode($data));

This is an example for your structure. Tweak it as you need.
The output of the above code will be:

{
"assistanceRequest": {
"type": "TECHNICAL_DATA",
"deliveryMode": "NORMAL",
"creationDate": "2020-04-09T10:09:00+02:00",
"source": "ATD",
"language": "FR",
"country": "FR"
},
"customer": {
"code": 123456,
"login": "client@company.com",
"companyName": "Workshop Harris",
"phoneNumber": "+44 123456789",
"email": "workshop@company.com"
}
}

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.



Related Topics



Leave a reply



Submit