简体   繁体   English

cURL发布JSON数组

[英]cURL post JSON array

Hi I'm having a problem with posting a JSON array with cURL to my API, 嗨,我在将带有cURLJSON数组发布到我的API时遇到问题,

I have this code below for the cURL post: 我在cURL帖子下面有以下代码:

$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? 它不存储/发布任何东西到API端,并且$ results返回的值不正确,代码有什么问题?

A bit from the JSON : 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"
}

Probably your $data_string is not in field=value pairs format and thus nothing is parsed in your $_POST global. 您的$data_string可能不是field=value对格式,因此在$_POST全局field=value没有任何内容。

Since you want to read from the $_POST global: 由于您要从$_POST全局读取:

  1. you should not set the content-type 您不应该设置content-type
  2. your $data_string must be in field=value pairs format 您的$data_string必须采用“ field=value对格式

The following will work (I have omitted altogether the header part, you should not set the content-type ): 以下将起作用(我完全省略了标题部分,不应设置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: 另一方面,如果您想访问已发送的数据,则不应尝试通过$_POST读取它们,而应在服务器端使用:

$JSONData = file_get_contents("php://input");

您需要对其进行http_build_query()

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

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