PHP fopen()和php://memory 未按预期工作(数据丢失)

PHP fopen()和php://memory 未按预期工作(数据丢失),php,curl,fopen,php-stream-wrappers,Php,Curl,Fopen,Php Stream Wrappers,我目前正在尝试将使用fopen()的类与php://memorystream以捕获卷曲头。总是有更好的方法来捕获Curl头,但是我现在没有足够的时间来编写自己的类 问题是没有捕获头信息。代码如下: $response_headers_fh = fopen('php://memory', 'wb+'); $ch->setOpt(CURLOPT_WRITEHEADER, $response_headers_fh); $ch->raw_response = cu

我目前正在尝试将使用
fopen()
的类与
php://memory
stream以捕获卷曲头。总是有更好的方法来捕获Curl头,但是我现在没有足够的时间来编写自己的类

问题是没有捕获头信息。代码如下:

    $response_headers_fh = fopen('php://memory', 'wb+');

    $ch->setOpt(CURLOPT_WRITEHEADER, $response_headers_fh);

    $ch->raw_response = curl_exec($ch->curl);

    rewind($response_headers_fh);
    $ch->raw_response_headers = stream_get_contents($response_headers_fh);
    fclose($response_headers_fh);
此时,在Curl请求之后,
$ch->raw\u response\u headers
属性为空。但是,如果指定文件路径:

    $response_headers_fh = fopen('/tmp/random_string', 'wb+');

    $ch->setOpt(CURLOPT_WRITEHEADER, $response_headers_fh);
$ch->raw\u response\u headers
属性设置正确

我们的PHP版本是:

$ php -v
PHP 5.3.3 (cli) (built: Sep 10 2014 07:51:23)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

有点难办…

curl无法写入
php://memory
stream。有一个古老的问题至今仍未解决。但您可以使用-选项来捕获标题:

function MyHeaderFunction($ch, $header)
{
    echo 'Header = ' . $header;
    return strlen($header);
}

$ch->setopt(CURLOPT_HEADERFUNCTION, 'MyHeaderFunction');

curl_exec($ch->curl);

我想你需要以
rwb
的形式打开它,而不是
wb+
。我试过了,它不起作用。