PHP CURLOPT_WRITEFUNCTION 似乎没有被调用
我正在尝试使用curl 读取数据流的一小块。
理想情况下,我只想检索流中的第一个图像并将其写入 jpg 文件。
我尝试使用 WRITEFUNCTION 来执行此操作,如果流的长度 > 则返回 -1比如说 20000。
function receiveResponse($ch,$string) {
$length = strlen($string);
if($length >= 20000) { return -1; }
return $length;
}
$ch = curl_init('http://<url>/videostream.cgi');
curl_setopt($ch, CURLOPT_USERPWD, '<user>:<password>');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, "receiveResponse");
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
然而,流只是继续写入文件,最终文件大小变得越来越大。
我该如何修复它?
I'm trying to read just one chunk of a stream of data using curl.
Ideally I would like to just retreive the first image in the stream and write that to a jpg file.
I'm attempting to do this using WRITEFUNCTION and returning -1 if the length of the stream > say 20000.
function receiveResponse($ch,$string) {
$length = strlen($string);
if($length >= 20000) { return -1; }
return $length;
}
$ch = curl_init('http://<url>/videostream.cgi');
curl_setopt($ch, CURLOPT_USERPWD, '<user>:<password>');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, "receiveResponse");
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
However the stream just continues to write to the file which ends up getting larger and larger in file size.
How can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们看看本手册中的选项说明 http://www.php.net /manual/en/function.curl-setopt.php:
所以,这意味着一个响应可以被分割成几块数据。为了正确接收前 20000 个字节,您必须添加 $full_length 计数器:
Lets look at option description from this manual http://www.php.net/manual/en/function.curl-setopt.php:
So, it means what a response can be split into several pieces of data. For appropriate receiving of first 20000 bytes you must add $full_length counter:
尝试评论这个curl_setopt($ch, CURLOPT_FILE, $fh);
try comment this curl_setopt($ch, CURLOPT_FILE, $fh);