0

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"
}
4
  • Are you sure your $data_string is formatted correctly? could you give us some example data to look at? Commented Aug 15, 2013 at 13:49
  • do you control the server you are making the call to? I would check the $_SERVER['REQUEST_METHOD'] as I find it strange that you used a custom request method (set to POST) instead of just setting curl_setopt($ch, CURLOPT_POST, 1); Commented Aug 15, 2013 at 14:02
  • Thanks @mishu but it the CURLOP_POST didn't do any difference in my results! No I'm not controlling the server Commented Aug 15, 2013 at 14:08
  • I added an extract from the JSON above that I'm trying to POST with this code, does it seem to be wrong? Commented Aug 15, 2013 at 14:09

2 Answers 2

1

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");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, unfurtunately it didn't help with urlencode(). Maybe the problem is at another place. Also I'm generating this JSON file with Jquery so it is never stored anywere, so therefor I'm using the POST
0

You need to http_build_query() it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.