PUTting data fields with PHP cURL
This is a little post about how to PUT multiple data fields using the PHP cURL extension. Why I wanted to do this in the first place is beyond the scope of this post, since its quite a long story. The curl command line allows data fields to be sent with a PUT request, and I wanted to do the same from PHP. Here is a snippet of code to show how I did it.
$data = array("a" => $a); $ch = curl_init($this->_serviceUrl . $id); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data)); $response = curl_exec($ch); if(!$response) { return false; } |
I'm putting this here so I remember for next time - if it helps you as well then even better :)

Thx !
Pingback: SugarCRM Developer Blog » Blog Archive » HOWTO: Do PUT requests with PHP cURL without writing to a file
You finally gave me the answer I was looking for with PUT variables - http_build_query(). Simple answer yet so hard to find! Thanks!
Brilliant!!! I'd been looking high and low for a SIMPLE example of using PUT with CURL in PHP but none of the examples worked or were way too complex for my needs. This little snipped of code was exactly what I needed to get some of my own code working :)
Many thanks for sharing...
thanks yar it works! :)
Thanks! Got really tired while trying to resolve that problem using standard documentation.
Thanks for taking the time to publish this! It was really helpful.
Thanks - very helpfull :)
Sometimes that's not enough, so you need more;
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
Link: http://tr.php.net/curl_setopt#109634
Different services may need all kinds of different headers, and you can set them using this CURLOPT_HTTPHEADER as shown here. I haven't seen that one before, but there are similar "features" in many APIs. Thanks for taking the time to add the code example.
Hi i am still not able to get any response ... i am using an external api which accepts put data .. plz help
Sorry, I'm not sure how to help since these examples worked for me. Perhaps you can log a ticket with your API provider? Good luck!
Awesome, can't believe this easy solution is so infrequently used ! Thanks so much for sharing.
it's just that nobody knows it's there, I think. Hope it was helpful!
millions of thanks, it solved my problem.
Thx ya. It Works.
Pingback: cURL PUT vs Zend_Rest_Controller - Zend Framework Forum - ZF1 / ZF2
Simple and working code. Thx.
Many Many And Many Thanks, for this articles. it's simple and also very helpful..
Thanks for this Post..
Have a good day..