请注意,如果您想使用 CURLOPT_PROGRESSFUNCTION 选项处理进度,则需要考虑您使用的是哪个版本的 PHP。从 5.5.0 版本开始,回调函数传递的参数数量/顺序发生了重大更改,并且 cURL 资源现在作为第一个参数传递。
5.5.0 版本之前
<?php
curl_setopt($resource, CURLOPT_PROGRESSFUNCTION, 'progressCallback');
curl_setopt($resource, CURLOPT_NOPROGRESS, false);
function progressCallback($download_size = 0, $downloaded = 0, $upload_size = 0, $uploaded = 0)
{
}
?>
5.5.0 版本及以后
<?php
curl_setopt($resource, CURLOPT_PROGRESSFUNCTION, 'progressCallback');
curl_setopt($resource, CURLOPT_NOPROGRESS, false);
function progressCallback($resource, $download_size = 0, $downloaded = 0, $upload_size = 0, $uploaded = 0)
{
}
?>
但是,如果您的代码需要与 5.5.0 之前和之后的 PHP 版本兼容,请考虑添加版本检查
<?php
curl_setopt($resource, CURLOPT_PROGRESSFUNCTION, 'progressCallback');
curl_setopt($resource, CURLOPT_NOPROGRESS, false);
function progressCallback($resource, $download_size = 0, $downloaded = 0, $upload_size = 0, $uploaded = 0)
{
if (version_compare(PHP_VERSION, '5.5.0') < 0) {
$uploaded = $upload_size;
$upload_size = $downloaded;
$downloaded = $download_size;
$download_size = $resource;
}
}
?>