|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43468 Curl doesn't handle php://memory stream
Submitted: 2007-12-01 10:00 UTC Modified: 2008-12-18 01:00 UTC
Avg. Score:4.2 ± 0.8
Reproduced:17 of 17 (100.0%)
Same Version:9 (52.9%)
Same OS:5 (29.4%)
From: peter at petersmit dot eu Assigned:
Status: No Feedback Package: cURL related
PHP Version: 5.2.6 OS: Ubuntu Linux Gutsy Gibbon
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-12-01 10:00 UTC] peter at petersmit dot eu
If you use a php://memory stream in combination with curl, nothing is written to the stream.

A filestream works fine.

Reproduce code:

$c = curl_init("");
$st = fopen("php://memory", "r+");

curl_setopt($c, CURLOPT_FILE, $st);

if(!curl_exec($c)) die ("error: ".curl_error($c));

echo "Content|".htmlspecialchars(stream_get_contents($st))."|/Content";


Expected result:
Content|The content of|/Content

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-13 22:16 UTC] quickshiftin at gmail dot com
i have discovered that this does work, partially, for some urls.
im not sure what is preventing this from working on all urls, but even for ones where it does work, the entire result is not placed in the buffer.
here is a modification of peters code, which illustrates 2 urls that work partially, one is the google translate 'api', the other is

#$c = curl_init("");
#$c = curl_init("");
$c = curl_init("");
$st = fopen('php://memory', 'r');

curl_setopt($c, CURLOPT_FILE, $st);
curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20080115 Firefox/');

if(!curl_exec($c)) die ("error: ".curl_error($c));

$str =  fgets($st);
echo stream_get_contents($st);
#echo "Content|".htmlspecialchars(stream_get_contents($st))."|/Content";
 [2008-12-10 01:55 UTC] dan dot hitt at gmail dot com
I can reproduce the bug.

I downloaded the link provided on the bug by jani (php 5.2 snapshot).

I built php (and had to also download and build libcurl).

My exact configuration line for the php was:
./configure --prefix=/home/danh/staging/php/2008_09_12_c --with-curl=/home/danh/staging/curl/2008_09_12
where the funky directory name is where i installed the curl i built.  (For the curl i used no options in the configuration except where to install it.)

My system is ubuntu 7.10.

It would be terrific to fix the bug because curl is so useful and having arbitrary streams is so useful: at least streams to memory.

Thanks everybody for your efforts on refining php.
 [2008-12-10 07:09 UTC] peter at petersmit dot eu
Sorry, not confirmed

I was checking it whit my own website address in the example code and everything looked fine. However with or it's not working!
 [2008-12-11 00:06 UTC]
Please try using this CVS snapshot:
For Windows:

And provide the full configure line you used if the snapshot does not 
 [2008-12-11 02:41 UTC] dan dot hitt at gmail dot com
I built again, and tried Peter's test again, and it failed again.

But note that with either the build from today or yesterday it
gives partial results for the wikipedia (it looks like it loses
the last 497 characters).

My configuration command is:

./configure --prefix=/home/danh/staging/php/2008_12_11_a --with-curl=/home/danh/staging/curl/2008_09_12

 [2008-12-18 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2009-01-29 00:06 UTC] davirtavares at gmail dot com
I got the same problem. I'm trying to submit data by PUT request and aparently memory streams don't work for just a single CURL option:

curl_setopt($ch, CURLOPT_INFILESIZE, $input_size);

Aparently some servers don't like when you submit data w/o the content-size.

But... how to obtain the size of a memory stream?

 [2009-01-29 02:14 UTC] davirtavares at gmail dot com
Well, I just discovered that I can use fstat to get the size of a stream (even memory ones).

Follows the test case:

$fi = fopen('php://temp', 'rb+');

// fill the $fi stream with some data

$input_stat = fstat($fi);
$input_size = $input_stat['size'];

curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $fi);
curl_setopt($ch, CURLOPT_INFILESIZE, $input_size);


 [2009-02-11 02:13 UTC] davirtavares at gmail dot com
The final workaround I currently use is setting both CURLOPT_WRITEFUNCTION and CURLOPT_READFUNCTION options. So I can write and read using the memory stream, even being unsupported by CURL:

// open the memory stream (currently using "temp" but it should works with "memory" stream as well):
$temp = fopen('php://temp', 'rb+');

function cb_curl_write($ch, $data) {
	global $temp;

	return fwrite($temp, $data);

function cb_curl_read($ch, $fd, $size) {
	global $temp;

	return fread($temp, $size);

// put some data into the stream:


// get the content size of the stream
$input_stat = fstat($temp);
$input_size = $input_stat['size'];

$ch = curl_init('');

curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILESIZE, $input_size);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'cb_curl_write');
curl_setopt($ch, CURLOPT_READFUNCTION, 'cb_curl_read');

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 16 20:01:29 2024 UTC