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($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, $length);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_PUT, 4);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Expect: '));
$response = curl_exec($ch);
fclose($fh);
add to del.icio.us
add to technorati favs
email this
June 23rd, 2007 at 5:29 pm
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?
June 25th, 2007 at 2:33 pm
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.
October 6th, 2007 at 2:46 am
Thank you for sharing!
November 6th, 2007 at 9:51 pm
[...] 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 [...]
March 3rd, 2008 at 3:23 pm
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()
May 17th, 2008 at 12:56 pm
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.
July 13th, 2008 at 3:38 pm
[...] http://osterman.com/wordpress/2007/06/13/php-curl-put-string [...]