Php 不过帐
我想在php中使用cURL发送json数据,但问题是cURL没有发布任何数据 注意:cURL已正确安装和配置Php 不过帐,php,curl,post,Php,Curl,Post,我想在php中使用cURL发送json数据,但问题是cURL没有发布任何数据 注意:cURL已正确安装和配置 $ch = curl_init($url); //The JSON data. $jsonData = '{ "recipient":{ "id":"'.$sender.'" }, "message":{ "text":"'.$message_to_reply.'" } }'; $jsonDataEncoded = $jsonData; //Tell cUR
$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);
json数据工作正常,但cURL post没有发布任何内容,也没有给出任何类型的警告/通知或错误
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'));
您可能不想将所有数据传递给一个键。打印的输出(数组($jsonDataEncoded))
打印输出(json解码(数组($JSONDATACENCODE))
据我所知,你犯了3个错误 1:不要做
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,“POST”)
,告诉curl您想要POST请求的正确方法是curl\u setopt($ch,CURLOPT\u POST,true)代码>
2:当您给CURLOPT_POSTFIELDS一个数组时,它实际上转换为多部分/表单数据
编码,这不是您想要的(您想要传输json)
3:您的$sender和$message_to_回复似乎刚刚插入到json原始文件中。如果您的$MasaGytoToRead包含一个<代码>“<代码> >或代码> < /COD>,它会使JSON无效。请考虑正确编码,例如使用JSONYEnEngy,如
$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 );
但是,假设$sender和$message_to_reply已经正确地进行了json编码,就我所见,您的原始代码不起作用的唯一原因是您给了CURLOPT_POSTFIELDS一个数组,因此,修复它所需要的只是从该行中删除“array”,如curl_setopt($ch,CURLOPT_POSTFIELDS,$jsonDataEncoded);
经过所有的尝试,以下是答案:
$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);
你怎么知道它不发布任何东西?而且,JSON很少是URL编码的,你确定你想要吗?最后,那个<代码> CurLopttCuppEdvest[/Cuff]使用不建议,只需删除它。@DanielStenberg,因为我使用的是ngrok,没有关于发布的统计信息。我还删除了urlencode(),但它仍然不起作用。不登录接收端不一定与“不发布任何内容”相同“虽然…@DanielStenberg还好,伙计,但是你知道为什么它不起作用吗?你能对你的站点执行这个cURL操作并给我们提供$u POST和$u GET的var_dump吗?它不会发布JSON,它会用多部分/表单数据编码发布数据(当你给cURL_setopt一个数组时,它会自动转换为多部分/表单数据)如果他想将json作为字符串传递,那么代码应该可以工作。
$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 );
$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);