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

39 thoughts on “PUTting data fields with PHP cURL

  1. Pingback: SugarCRM Developer Blog » Blog Archive » HOWTO: Do PUT requests with PHP cURL without writing to a file

  2. You finally gave me the answer I was looking for with PUT variables – http_build_query(). Simple answer yet so hard to find! Thanks!

  3. 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…

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

    • 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!

  4. Pingback: cURL PUT vs Zend_Rest_Controller - Zend Framework Forum - ZF1 / ZF2

  5. Many Many And Many Thanks, for this articles. it’s simple and also very helpful..

    Thanks for this Post..

    Have a good day..

  6. Pingback: SugarCRM Developer Blog » Blog Archive » Doing PUT and DELETE with CURL in PHP

  7. Pingback: Doing PUT and DELETE with CURL in PHP « SugarCRM Developer Blog

  8. How would one change your script to send an XML file; your code seems to be building the http_post code
    Any help will be appreciated

    By the way a grat write up..

  9. Thank you so much for posting this. I’ve been banging my head against this issue for several hours.
    Seems crazy that
    [code]curl_setopt($ch, CURLOPT_PUT, true);[/code]
    and
    [code]curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “PUT”);[/code]
    should produce identical results … but somehow the first one doesn’t work and the second one does.

  10. In reply to Andy’s comment, I think you can get CURLOPT_PUT to work if you provide the data using INFILE instead of POSTFIELDS. For example:
    [code]
    curl_setopt($ch, CURLOPT_PUT, true);
    curl_setopt($ch, CURLOPT_INFILE, $putData);
    curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putString));
    [/code]
    I would guess the reason is that POST can be used with either form data or a payload, but using PUT with form data doesn’t make sense.

  11. Some suggestion about putting a pdf file into CouchDB from php. Hope to be useful.

    // url to CouchDB server
    $url_path_str = ‘http://address-of-my-couchdb-server:5984/pdfdb/MYCODE/MYFILENAME.pdf’;

    // path to the pdf file to insert into CouchDB database “pdfdb”
    $file_path_str = ‘/path to my file.pdf’;

    // header – please note that “Content-Type” is NOT “application/json” like used in other POST/GET calls with JSON data
    $header = array(
    “Content-type: application/pdf”,
    “Cache-Control: no-cache”,
    “Pragma: no-cache”
    );

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url_path_str);
    curl_setopt($ch, CURLOPT_PUT, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “PUT”);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    $fh_res = fopen($file_path_str, ‘r’);
    $file_data_str = fread($fh_res, filesize($file_path_str));
    rewind($fh_res);

    curl_setopt($ch, CURLOPT_INFILE, $fh_res);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $curl_response_res = curl_exec ($ch);
    // the answer is similar to {“ok”:true,”id”:”MYCODE”,”rev”:”3-6e8cb7a70206c31a8f0dab27b3b25353″}

    curl_close($ch);

    fclose($fh_res);

    // Warning : CouchDB requires that last document revision must be provided during document update

    • Great this worked for me, one thing I noticed is that you do not have to specify a content type for uploading every file is treated as binary chunks hence if your server stores the file exactly as it is unless your server is looking for a specific MIME type.

  12. Another userful example which help me

    $data = array(
    “number” => “3dde3379-e38a-104c-089f-53b554ed5dfa”,
    “email” => “[email protected]”,

    );

    $json_data = json_encode($data);

    $headers[] = ‘Content-Type: application/json’;
    $headers[] = ‘Content-Length: ‘ . strlen($json_data);

    $ch = curl_init($url);

    //That two line is very important for PUT!
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “PUT”);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $content = curl_exec($ch);
    $info = curl_getinfo($ch);

    curl_close($ch);

  13. I am curious why you did not include [code]curl_close($ch)[/code] at the end of your code snippet for good measure. I get that it isn’t exactly necessary but I am a firm believer that it is good coding practice.

    • I agree that the curl_close($ch) should be included. This post is 8 years old so I am afraid I can’t satisfy your curiosity about why it’s not there in the first place, sorry. Probably carelessness or an attempt to keep the post focussed??

  14. HI,

    Many thanks for the code.

    As already mentioned by others, the original code was not working in my case. I just had to change one thing and everything works perfectly.

    Instead
    [code]
    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
    [/code]

    I had to write:
    [code]
    curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
    [/code]

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.