Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中的cURL下载进度不正常?_Php_Curl_Scripting - Fatal编程技术网

PHP中的cURL下载进度不正常?

PHP中的cURL下载进度不正常?,php,curl,scripting,Php,Curl,Scripting,我是一名PHP新手,尝试使用以下方法向现有PHP脚本添加进度条: $ch=curl_init() or die("ERROR|<b>Error:</b> cURL Error"); curl_setopt($ch, CURLOPT_URL, $c); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_FILE, $fp); //########################

我是一名PHP新手,尝试使用以下方法向现有PHP脚本添加进度条:

$ch=curl_init() or die("ERROR|<b>Error:</b> cURL Error");
curl_setopt($ch, CURLOPT_URL, $c);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_FILE, $fp);

//####################################################//
// This is required to curl give us some progress
// if this is not set to false the progress function never
// gets called
curl_setopt($ch, CURLOPT_NOPROGRESS, false);

// Set up the callback
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'callback');

// Big buffer less progress info/callbacks
// Small buffer more progress info/callbacks
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
//####################################################//

curl_exec($ch);
curl_close($ch);
fclose($fp);
现在,我已经从PHP网站上复制粘贴了这个例子,但这不起作用??我的PHP版本是5.2.11。建议可能有什么问题

编辑:我从另一个脚本调用此php脚本。


信息:我被5.2.X分支卡住了,因为我的web主机说cPanel还不支持5.3.X分支,有什么解决方案吗

查看一下,您将发现两个条目
-[DOC]MFH:#41712,实现进度回调
。一个用于php5.3,一个用于php6分支

编辑:使用PHP5.2.x,您应该能够设置


关于最后一条注释,所述代码需要5.3,因为stream_context_create()的第二个参数是在5.3中添加的。但是,在5.2中用以下工程替换该线路:

$ctx = stream_context_create();
stream_context_set_params($ctx, array("notification" => "stream_notification_callback"));
在相关新闻中,PHP手册中的stream_notification_callback()文档有一个充分利用/创建进度条的示例,请查看


复制粘贴不是学习PHP或任何其他编程语言的方法。我同意,我只是搜索了一下,看到这个示例最终使用了它-我不认为这有什么错。:)如果有人想使用此代码,请记住,在第一次或前几次调用中,$download\u size可以为零。因此,请确保在$percent=$download/$download\u size中缓存零除错误,或者在计算之前检查$download\u size>0。谢谢,我会让我的主机更新php,看看这是否有效。我被5.2.X分支卡住了,因为我的web主机说cPanel还不支持5.3.X分支,对此有什么解决方案吗?顺便说一句,在回显后不要忘记刷新:php.net/manual/en/function.ob-flush.phpIf不使用curl是可以接受的流api也提供通知回调。从PHP5.2开始就有了。请参见编辑。
function foo() {
  $args = func_get_args();
  echo join(', ', $args), "\n";
}

$ctx = stream_context_create(null, array('notification' =>'foo'));
$fpIn = fopen('http://php.net/', 'rb', false, $ctx);
file_put_contents('localfile.txt', $fpIn);
$ctx = stream_context_create();
stream_context_set_params($ctx, array("notification" => "stream_notification_callback"));