Reputation: 432
I want to send json data using cURL in php, but the problem is that cURL is not posting any data.
NOTICE: cURL is properly installed and configured.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"'.$message_to_reply.'"
}
}';
$jsonDataEncoded = $jsonData;
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, array($jsonDataEncoded));
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_exec($ch);
The json Data is working fine but the cURL post is not posting anything and also not giving any type of warnings/notice or error.
Upvotes: 0
Views: 721
Reputation: 432
Well after all the try, Here is the answer:
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"'.$message_to_reply.'"
}
}';
$jsonDataEncoded = $jsonData;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Here i removed the array//
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// By default in PHP7 CURL_SSL_VERIFYPEER, is true. You have to make it false//
$result = curl_exec($ch);
Upvotes: 0
Reputation: 21513
as far as i can see, you do 3 mistakes
1: don't do curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
, the correct way to tell curl that you want a POST request is curl_setopt($ch, CURLOPT_POST, true);
2: when you give CURLOPT_POSTFIELDS an array, its actually converted to a multipart/form-data
encoding, which is not what you want (you want to transfer a json)
3: your $sender and $message_to_reply seem to be just inserted in to the json raw. what happens if your $message_to_reply contains an "
or '
? it will invalidate the json. consider encoding it properly, for example using json_encode, like
$jsonData = array (
'recipient' => array (
'id' => $sender
),
'message' => array (
'text' => $messaage_to_reply
)
);
$jsonDataEncoded = json_encode ( $jsonData, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
but, provided that $sender and $message_to_reply is already properly json encoded, the only reason your original code doesn't work, as far as i can see, is that you give CURLOPT_POSTFIELDS an array, thus, all that's needed to fix it would be to remove "array" from that line, like curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonDataEncoded);
Upvotes: 1
Reputation: 828
Try this;
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(json_decode($jsonDataEncoded)));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
You probably don't want to pass all data to one key.
Output of print_r(array($jsonDataEncoded))
Array ( [0] => { "recipient":{ "id":"me" }, "message":{ "text":"hello" } } )
Output of print_r(json_decode(array($jsonDataEncoded)))
Array ( [0] => stdClass Object ( [recipient] => stdClass Object ( [id] => me ) [message] => stdClass Object ( [text] => hello ) ) )
Upvotes: 0