简体   繁体   中英

cURL post JSON array

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"
}

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

您需要对其进行http_build_query()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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