Using PHP and curl with http PUT on string data

This wasn't obvious

There are various other posts on this - but mostly assuming you will put a file.

In my case I want to put the contents of a string so I needed to craete a temporary filehandle.

<?php
function put_it($url, $string) {
 
$ch = curl_init($url);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
curl_setopt($ch, CURLOPT_PUT, TRUE);
 
// create tempoary file handle
 
$oneMB = 1024 * 1024;
 
$fp = fopen("php://temp/maxmemory:$oneMB", 'r+');
 
fputs($fp, $string);
 
rewind($fp);
 
curl_setopt($ch, CURLOPT_INFILE$fp);
 
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($string));
 
$response = curl_exec($ch);
 
fclose($fp);
?>

Tags

Post new comment

Got something to add - just enter a comment
all other fields are optional.

Your email address will not be published.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image without spaces, also respect upper and lower case.