duanliaoyu8419 2017-01-17 07:18
浏览 10

PHP cUrl没有发布

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.

  • 写回答

3条回答 默认 最新

  • douqiao5552 2017-01-17 08:40
    关注

    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 ) ) )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测