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
Why is posting JSON with PHP Curl not working?
I found the problem with help from this link:
file_get_contents('php://input') always returns an empty string
Because I used curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
I could not see that there was a redirect from www.url.com to url.com.
When a request is redirected you lose all data in file_get_contents("php://input")
send post json with php (curl)
There is the following error - the phone number has to be string, not number, so it would look like this 'phone' => '08123456789'
, because numbers cannot begin with 0.
Beside this, there is the following issue - You should not set item_details like this, but rather 'item_details' => [$item]
. You do not need 2 nested arrays, just one. (array()
is equal to []
. They are practically the same in your use case. So you are doing something like array(array($item))
, which is wrong)
What else, you have to do a $json = json_encode($data2);
at the end and it will return what you want it to.
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.
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);
Related Topics
How to Use PHP to Dynamically Publish an Ical File to Be Read by Google Calendar
Programmatically Access Currency Exchange Rates
What Are the Differences Between Psr-0 and Psr-4
Understanding Ioc Containers and Dependency Injection
Alter Table in Magento Setup Script Without Using SQL
How to Throttle My Site's API Users
Composer Killed While Updating
How to Force Users to Access My Page Over Https Instead of Http
How to Create Static Classes in PHP (Like in C#)
Why Is Textarea Filled with Mysterious White Spaces
How to Easily Switch Between PHP Versions on MAC Osx
Sanitizing User's Data in Get by PHP