如何使用php://内存文件句柄做cURL PUT请求


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

我使用第三方PHP类访问API,它有以下代码:

$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);

在最后一行,也就是这一行:

curl_setopt($ch, CURLOPT_INFILE, $fh);

我得到错误:

警告:curl_setopt() [function。Curl-setopt]:不能表示a作为STDIO文件的内存类型流*

我做错了什么?

您的内存文件句柄仅为写入(w+)打开。增加读取,例如尝试为文件句柄设置rw+

另一种选择是使用php://temp代替内存。如果没有足够的可用内存,这将写入临时文件。