Reputation: 7889
I am posting an array converted to JSON
string with cURL
like below.
However on the receiving end, I get an empty array.
function postFunction ($data_h) {
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => 'https://domain.tld/script.php',
CURLOPT_HTTPHEADER => array('Content-Type:application/json'),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data_h),
CURLOPT_RETURNTRANSFER => true
)
);
return curl_exec($c);
curl_close($c);
}
$setup = [
'sms' => [
'number' => '+39XXXXXXXX',
'message' => 'Sample msg 123...',
]
];
print_r( postFunction($setup) );
Instead of $_POST
I've tried using var_dump($HTTP_RAW_POST_DATA)
as I saw on another question on Stack—but all I get with it is a NULL
.
This is what I get when I return curl_getinfo($c)
:
It doesn't even adjust the content type.
Array
(
[url] => https://domain.tld/script.php
[content_type] => text/html; charset=utf-8
[http_code] => 200
[header_size] => 140
[request_size] => 593
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.127732
[namelookup_time] => 0.013128
[connect_time] => 0.013257
[pretransfer_time] => 0.106738
[size_upload] => 463
[size_download] => 5
[speed_download] => 39
[speed_upload] => 3624
[download_content_length] => -1
[upload_content_length] => 0
[starttransfer_time] => 0.125601
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => xx.xx.xx.xx
[certinfo] => Array
(
)
)
At the end I want to turn the data into an object with json_decode()
however I first need to get the data.
Any thoughts on what I'm doing incorrectly here?
Upvotes: 2
Views: 1065
Reputation: 56
In your code you are indicating the content-type to be json, but your POST data isn't really all json. You can use
file_get_contents("php://input")
To get your data, instead of $_POST
Upvotes: 1