I'm using a REST API through PHP and I'm a bit confused on the CURL_POSTFIELDS behavior, which implies that I must send a querystring or array, but what I'm seeking to do is just to send a string of JSON text.

In the API I'm using there is no such thing as a field: https://developer.atlassian.com/display/JIRADEV/JIRA+REST+API+Example+-+Edit+issues

So, how can I just send data that doesn't get interpreted as a query string, and without having to use a file?

  • 1
    Try: curl_setopt($ch, CURLOPT_POST, TRUE); – yannis May 27 '13 at 20:28
  • Actually, I'm using CURLOPT_CUSTOMREQUEST with $_SERVER['REQUEST_METHOD'] to handle custom methods, the method is not the problem – dukeofgaming May 27 '13 at 20:38
  • If you don't set CURLOPT_POST to true, you aren't sending a regular (application/x-www-form-urlencoded) POST request. Try it out and see what happens. Reading your question a second time, I think I'm completely missing its point. Care to be a bit more specific on exactly what you are trying to do and why you even looked at CURL_POSTFIELDS? All the API's examples are PUT requests, not POST ones. – yannis May 27 '13 at 20:41

Turns out all I needed was:

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

That way the content sent in the POSTFIELDS gets interpreted the right way. Source: http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl

| improve this answer | |

I found an article about using JSON with PHP/CURL

In general, we do pretty much this but with an XML-RPC request:

curl_setopt($cnxn, CURLOPT_POST, TRUE);
curl_setopt($cnxn, CURLOPT_POSTFIELDS, $body);

We just build the XML document for the RPC request into the $body variable, and our server will read it as the request body. We use Zend Framework MVC, but you can read the incoming raw request body from the php://input stream if you're rolling your own.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.