my life

day to day

PHP CURL PUT String

June 13th, 2007

In the process of writing a MogileFS client in PHP, I discovered there’s no no straight forward way to issue a PUT request using CURL where the body of the request comes from a string, rather than a file. Luckily, PHP 5.2 introduces memory based streams…

 

   $fh 
fopen('php://memory''rw');
    fwrite($fh$dataToPut);
    rewind($fh);
   $ch curl_init();
    curl_setopt($chCURLOPT_INFILE$fh);
    curl_setopt($chCURLOPT_INFILESIZE$length);
    curl_setopt($chCURLOPT_TIMEOUT10);
    curl_setopt($chCURLOPT_PUT4);
    curl_setopt($chCURLOPT_URL$url);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_HTTPHEADER, Array('Expect: '));
    $response curl_exec($ch);
    fclose($fh);

add to del.icio.us    add to technorati favs    email this

7 Responses to “PHP CURL PUT String”

  1. Billy Extern Says:

    did you write the php mog client from scratch? I am looking for one and I found one here:

    http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/MogileClient/MogileFS.php?view=markup

    is yours better or more complete somehow and if so, will you release it?

  2. e Says:

    I do want to release my MogileFS class since there are no other robust MogileFS implementations. The Mediawiki one you found is really lacking and has a few bugs. I’ll keep you posted as soon as it becomes available.

  3. Wahoo Says:

    Thank you for sharing!

  4. John Dusbabek Says:

    [...] a PUT request, specifically where the body comes from a local string variable. I found a solution here (it basically says all the stuff I just said, with a code example. Here’s my code example [...]

  5. Ojobazos Says:

    Try this - much easier way :
    function doPut($ch, $a_data) {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘PUT’);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(’Content-Length: ‘.strlen($a_data));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $a_data));
    return exec($ch);
    } //doPut()

  6. Joe Stump Says:

    Steve Williams from Digg wrote File_Mogile when we put MogileFS into production at Digg. You can find the proposal at http://pear.php.net/pepr/pepr-proposal-show.php?id=528 He should be releasing it soon.

  7. RESTful PUT calls with PHP and Curl :: Jaisen Mathai Says:

    [...] http://osterman.com/wordpress/2007/06/13/php-curl-put-string [...]

Leave a Reply