11

I need to POST the following JSON code, but for some reason it is not working. Below is the code that I have.

$fieldString = "395609399";
//the curl request processor
function processCurlJsonrequest($URL, $fieldString) { //Initiate cURL request and send back the result
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_cookie_file_path);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    if ($fieldCount) { // in case of post fields present then pass it on
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{categoryId: $fieldString}"));
        curl_setopt($ch, CURLOPT_POST, 1);
    }
    $resulta = curl_exec($ch);
    if (curl_errno($ch)) {
        print curl_error($ch);
    } else {
        curl_close($ch);
    }
    return $resulta;
}

Here is the function which calls the cURL request:

function get_cat($categoryId, $URL) {
    $fields = array(
        "categoryId" => $categoryId
    );
    $fields_string = $fields;
    return $this->processCurlJsonrequest($URL, $fields_string);
}
3
  • 1
    Give json_encode a PHP array, not a string that is already in the form of a JS object. Commented Nov 24, 2010 at 21:47
  • I've tried curl_setopt($ch, CURLOPT_POSTFIELDS, array(json_encode(array( "categoryId"=>"5016")))); and json_encode(array( "categoryId"=>"5016"))); and is not working either Commented Nov 25, 2010 at 0:25
  • 3
    It's super annoying that you apparently solved the problem (as indicated by your comment) but did not update the post to indicate what the solution was. Commented Mar 25, 2012 at 18:26

5 Answers 5

19

The bit that is the problem is:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{categoryId: $fieldString}"));

CURLOPT_POSTFIELDS will accept either an array of parameters, or a URL-encoded string of parameters:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('json'=>json_encode($stuff)));
curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.urlencode(json_encode($stuff)));

Where json will be the name of the POST field (i.e.: will result in $_POST['json'] being accessible).

Sign up to request clarification or add additional context in comments.

4 Comments

for some reasons is not working . I'm getting {"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}Array ( [Message] => There was an error processing the request. [StackTrace] => [ExceptionType] => ) . I think is because the post is not made properly .
I have uploaded a picture on this link so you can see the exactly post request from http analyzer that I'm trying to replicate with php curl . img502.imageshack.us/img502/1346/analyzer.jpg
as you can see in the http analyzer don't appear any post field (e.g "json")
@Michael the server your are posting has set a security feature that doesn't accept ordinary PHP post request generated by POSTFIELDS. You will need to put the body code in a file and upload the file via php. I was having a similar problem.
9

Send your text without JSON encoding, and add this header code:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8","Accept:application/json, text/javascript, */*; q=0.01")); 

Comments

7

With the initial example, working code should be like this:

//the curl request processor
function processCurlJsonrequest($URL, $fieldString) { //Initiate cURL request and send back the result
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_cookie_file_path);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("myJsonData" => "test")));
    curl_setopt($ch, CURLOPT_POST, 1); 
    $resulta = curl_exec($ch);
    if (curl_errno($ch)) {
        print curl_error($ch);
    } else {
        curl_close($ch);
    }
    return $resulta;
}

Comments

4

I had issues sending JSON via cURL, and the problem was that I was not explicitly setting the content length in the header.

So the Header should be:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($json)));

Comments

3

It's pretty simple really, make sure you've set the extra Content-Type header set for json, and then CURLOPT_POSTFIELDS can take the json as a string. No encoding necessary.

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.