php使用curl下载指定大小的文件实例代码
(编辑:jimmy 日期: 2025/11/5 浏览:3 次 )
php中使用基于libcurl的curl函数,可以对目标url发起http请求并获取返回的响应内容。通常的请求方式类似如下的代码:
public function callFunction($url, $postData, $method, header='')
{
$maxRetryTimes = 3;
$curl = curl_init();
/******初始化请求参数start******/
if(strtoupper($method) !== 'GET' && $postData){
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($postData));
}elseif (strtoupper($method) === 'GET' && $postData){
$url .= '"http://php.net/manual/en/function.curl-setopt-array.php" rel="external nofollow" >http://php.net/manual/en/function.curl-setopt-array.php ,最终使用了CURLOPT_WRITEFUNCTION参数设置了on_curl_write,该函数将会1s中被回调1次。
$ch = curl_init();
$options = array(CURLOPT_URL => 'http://www.php.net/',
CURLOPT_HEADER => false,
CURLOPT_HEADERFUNCTION => 'on_curl_header',
CURLOPT_WRITEFUNCTION => 'on_curl_write'
);
最终我的实现片段:
function on_curl_write($ch, $data)
{
$pid = getmypid();
$downloadSizeRecorder = DownloadSizeRecorder::getInstance($pid);
$bytes = strlen($data);
$downloadSizeRecorder->downloadData .= $data;
$downloadSizeRecorder->downloadedFileSize += $bytes;
// error_log(' on_curl_write '.$downloadSizeRecorder->downloadedFileSize." > {$downloadSizeRecorder->maxSize} \n", 3, '/tmp/hyb.log');
//确保已经下载的内容略大于最大限制
if (($downloadSizeRecorder->downloadedFileSize - $bytes) > $downloadSizeRecorder->maxSize) {
return false;
}
return $bytes; //这个不正确的返回,将会报错,中断下载 "errno":23,"errmsg":"Failed writing body (0 != 16384)"
}
DownloadSizeRecorder是一个单例模式的类,curl下载时记录大小,实现返回下载内容的md5等。
class DownloadSizeRecorder
{
const ERROR_FAILED_WRITING = 23; //Failed writing body
public $downloadedFileSize;
public $maxSize;
public $pid;
public $hasOverMaxSize;
public $fileFullName;
public $downloadData;
private static $selfInstanceList = array();
public static function getInstance($pid)
{
if(!isset(self::$selfInstanceList[$pid])){
self::$selfInstanceList[$pid] = new self($pid);
}
return self::$selfInstanceList[$pid];
}
private function __construct($pid)
{
$this->pid = $pid;
$this->downloadedFileSize = 0;
$this->fileFullName = '';
$this->hasOverMaxSize = false;
$this->downloadData = '';
}
/**
* 保存文件
*/
public function saveMaxSizeData2File(){
if(empty($resp_data)){
$resp_data = $this->downloadData;
}
$fileFullName = '/tmp/http_'.$this->pid.'_'.time()."_{$this->maxSize}.download";
if($resp_data && strlen($resp_data)>0)
{
list($headerOnly, $bodyOnly) = explode("\r\n\r\n", $resp_data, 2);
$saveDataLenth = ($this->downloadedFileSize < $this->maxSize) "htmlcode">
……
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'on_curl_write');//设置回调函数
……
$pid = getmypid();
$downloadSizeRecorder = DownloadSizeRecorder::getInstance($pid);
$downloadSizeRecorder->maxSize = $size_limit;
……
//发起curl请求
$response = curl_exec($ch);
……
//保存文件,返回md5
$downloadSizeRecorder->saveMaxSizeData2File(); //保存
$downloadFileMd5 = $downloadSizeRecorder->returnFileMd5();
$downloadedfile_size = $downloadSizeRecorder->returnSize();
$downloadSizeRecorder->deleteFile();
到这里,踩了一个坑。增加了on_curl_write后,$response会返回true,导致后面取返回内容的时候异常。好在已经实时限制了下载的大小,用downloadData来记录了已经下载的内容,直接可以使用。
if($response === true){
$response = $downloadSizeRecorder->downloadData;
}
总结
以上所述是小编给大家介绍的php使用curl下载指定大小的文件,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
下一篇:ThinkPHP3.1.x修改成功与失败跳转页面的方法