Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do cURL PUT requests with a php://memory file handle?

I'm using a 3rd party PHP class to access an API, it has got the following code:

$fh = fopen('php://memory', 'w+');
fwrite($fh, $xml);
rewind($fh);
$ch = curl_init($req->to_url() );
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $fh);

On the last line, i.e this one:

curl_setopt($ch, CURLOPT_INFILE, $fh);

I'm getting the error:

Warning: curl_setopt() [function.curl-setopt]: cannot represent a stream of type MEMORY as a STDIO FILE*

What am I doing wrong?

like image 445
Ali Avatar asked Dec 13 '22 10:12

Ali


1 Answers

Your Memory File Handle is only open for writing (w+). Add reading, e.g. try to set rw+ for the file handle.

An alternative would be to use php://temp instead of memory. That would write to a temporary file if there isn't enough memory available.

like image 131
Gordon Avatar answered Jan 09 '23 19:01

Gordon