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);
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Michael
  • 6,377
  • 14
  • 59
  • 91
  • 1
    Give json_encode a PHP array, not a string that is already in the form of a JS object. – JAL Nov 24 '10 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 – Michael Nov 25 '10 at 00: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. – shanusmagnus Mar 25 '12 at 18:26

5 Answers5

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).

netcoder
  • 66,435
  • 19
  • 125
  • 142
  • 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 . – Michael Nov 24 '10 at 23:43
  • 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 . http://img502.imageshack.us/img502/1346/analyzer.jpg – Michael Nov 24 '10 at 23:46
  • 2
    as you can see in the http analyzer don't appear any post field (e.g "json") – Michael Nov 24 '10 at 23:53
  • 1
    @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. – James Wayne Oct 07 '16 at 13:08
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")); 
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Atef
  • 593
  • 1
  • 8
  • 18
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;
}
jussil
  • 71
  • 1
  • 1
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)));
jazzdrive3
  • 335
  • 3
  • 13
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.

Flexo
  • 87,323
  • 22
  • 191
  • 272
mal
  • 62
  • 4