Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
Try: curl_setopt($ch, CURLOPT_POST, TRUE); – Yannis Rizos May 27 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 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 Rizos May 27 at 20:41

2 Answers

up vote 1 down vote accepted

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

share|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_RETURNTRANSFER, 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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