简体   繁体   English

PHP cUrl不发布

[英]PHP cUrl not posting

I want to send json data using cURL in php, but the problem is that cURL is not posting any data. 我想使用cURL在php中发送json数据,但是问题是cURL没有发布任何数据。

NOTICE: cURL is properly installed and configured. 注意:cURL已正确安装和配置。

$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. json数据工作正常,但cURL帖子未发布任何内容,也未给出任何类型的警告/通知或错误。

as far as i can see, you do 3 mistakes 据我所知,你犯了3个错误

1: don't do curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 1:不要执行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); ,告诉curl您想要POST请求的正确方法是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) 2:当您给CURLOPT_POSTFIELDS一个数组时,它实际上转换为multipart/form-data编码,这不是您想要的(您要传输json)

3: your $sender and $message_to_reply seem to be just inserted in to the json raw. 3:您的$ sender和$ message_to_reply似乎只是插入到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 如果$ message_to_reply包含"'什么,它将使json失效。请考虑对其进行正确编码,例如使用json_encode,例如

$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); 但是,只要$ sender和$ message_to_reply已经正确进行json编码,就我所知,您的原始代码不起作用的唯一原因是,您给CURLOPT_POSTFIELDS一个数组,因此,修复它所需的所有操作是从该行中删除“数组”,例如curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonDataEncoded);

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)) 输出print_r(array($jsonDataEncoded))

Array ( [0] => { "recipient":{ "id":"me" }, "message":{ "text":"hello" } } ) 


Output of print_r(json_decode(array($jsonDataEncoded))) print_r(json_decode(array($jsonDataEncoded)))

Array ( [0] => stdClass Object ( [recipient] => stdClass Object ( [id] => me ) [message] => stdClass Object ( [text] => hello ) ) )

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM