Sta*_*ked 4 php scripting curl
我是一个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);
Run Code Online (Sandbox Code Playgroud)
回调函数:
function callback($download_size, $downloaded, $upload_size, $uploaded)
{
$percent=$downloaded/$download_size;
// Do something with $percent
echo "$percent";
}
Run Code Online (Sandbox Code Playgroud)
现在,我从PHP网站上直接复制粘贴了这个例子,但这不起作用?我的PHP版本是5.2.11,请.提出什么可能是错的?
编辑:我从另一个脚本调用这个PHP脚本.
信息:我坚持5.2.X分支,因为我的网络主机说cPanel不支持5.3.x分支,任何解决方案?
在php 5.3之前似乎没有CURLOPT_PROGRESSFUNCTION.
看看http://cvs.php.net/viewvc.cgi/php-src/ext/curl/interface.c?view=log,你会发现两个条目- [DOC] MFH: #41712, implement progress callback
.一个用于php5.3,另一个用于php6分支.
编辑:使用php 5.2.x,您应该能够设置stream_notification_callback
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);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7529 次 |
最近记录: |