dongshao1156 2013-08-15 05:44
浏览 33
已采纳

cURL发布JSON数组

Hi I'm having a problem with posting a JSON array with cURL to my API,

I have this code below for the cURL post:

$data_string = stripslashes($JSONData);

$ch = curl_init('http://api.webadress.com');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(  
    'Accept: application/json',                                                                                                                                                        
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

It doesn't store/post anything to the API end and the $results is not returning correct, What is wrong with the code?

A bit from the JSON:

{
"name": "test",
"type_id": "1",
"css": "#fb-iframe{}#fb-beforelike{}#fb-beforelike-blur{}",
"json": [
    {
        "Canvas": [
            {
                "Settings": {
                    "Page": {
                        "campaignName": "test"
                    }
                },
                "QuizModule": {
                    "Motivation": [],
                    "Questions": [],
                    "Submit_Fields": [
                        {
                            "label": "Name",
                            "name": "txtName",
                            "value": true
                        }
                    ]
                }
            }
        ]
    }
],
"user_id": "123"
}

展开全部

  • 写回答

2条回答 默认 最新

  • dongyin8991 2013-08-15 06:12
    关注

    Probably your $data_string is not in field=value pairs format and thus nothing is parsed in your $_POST global.

    Since you want to read from the $_POST global:

    1. you should not set the content-type
    2. your $data_string must be in field=value pairs format

    The following will work (I have omitted altogether the header part, you should not set the content-type):

    $data_string = stripslashes($JSONData);
    $ch = curl_init('http://api.webadress.com');   
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('JSONData'=>$data_string));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    

    If on the other hand you want to access the data as you already send them, then you shouldn't try to read them through $_POST but instead use on the server side:

    $JSONData = file_get_contents("php://input");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 传人记程序做的plc 485从机程序该如何写
  • ¥15 已知手指抓握过程中掌指关节、手指各关节和指尖每一帧的坐标,用贝塞尔曲线可以拟合手指抓握的运动轨迹吗?
  • ¥50 libwebsockets 如何添加其他socket事件回调
  • ¥50 实现画布拖拽算子排布,通过flink实现算子编排计算,请提供思路
  • ¥15 esium自定义材质拉伸问题
  • ¥15 cmake+mingw使用<mysqlx/xdevapi.h>报错
  • ¥15 eNSP中防火墙的使用
  • ¥15 关于#mlnet#的问题:mlnet相关请求(语言-c#)
  • ¥15 lvgl7.11怎么做出文字被选中的效果
  • ¥50 如何快速查看手机目标app的主要服务器ip
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部