当前位置:首页 > PHP教程 > php上传下载 > 列表

php使用curl下载指定大小的文件实例代码

发布:smiling 来源: PHP粉丝网  添加日期:2021-08-11 13:25:37 浏览: 评论:0 

这篇文章主要介绍了php使用curl下载指定大小的文件,需要的朋友可以参考下,php中使用基于libcurl的curl函数,可以对目标url发起http请求并获取返回的响应内容,通常的请求方式类似如下的代码:

  1. public function callFunction($url, $postData, $method, header='') 
  2. { 
  3.   $maxRetryTimes = 3; 
  4.   $curl = curl_init(); 
  5.   /******初始化请求参数start******/ 
  6.   if(strtoupper($method) !== 'GET' && $postData){ 
  7.     curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($postData)); 
  8.   }elseif (strtoupper($method) === 'GET' && $postData){ 
  9.     $url .= '?'. http_build_query($postData); 
  10.   } 
  11.   /******初始化请求参数end******/ 
  12.   curl_setopt_array($curl, array( 
  13.     CURLOPT_URL => $url, 
  14.     CURLOPT_TIMEOUT => 10, 
  15.     CURLOPT_NOBODY => 0, 
  16.     CURLOPT_RETURNTRANSFER => 1 
  17.   )); 
  18.   if(method == 'POST'){ 
  19.     curl_setopt($curl, CURLOPT_POST, true); 
  20.   } 
  21.   if(false == emptyempty()){ 
  22.     curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
  23.   } 
  24.   $response = false; 
  25.   while(($response === false) && (--$maxRetryTimes > 0)){ 
  26.     $response = trim(curl_exec($curl)); 
  27.   } 
  28.   return $response; 
  29. } 

上面代码中的这个$response是curl发起的这次http请求从$url获取到的数据,如果没有在$header中通过range来指定要下载的大小,无论这个资源多大,那么都要请求完整的并返回的是这个URI的完整内容。通常只用curl来请求求一些接口或者远程调用一个函数获取数据,,所以这个场景下CURLOPT_TIMEOUT这个参数很重要。

对于curl的使用场景不止访问数据接口,还要对任意的url资源进行检测是否能提供正确的http服务。当用户填入的url是一个资源文件时,例如一个pdf或者ppt之类的,这时候如果网络状况较差的情况下用curl请求较大的资源,将不可避免的出现超时或者耗费更多的网络资源。之前的策略是完全下载(curl会下载存储在内存中),请求完后检查内容大小,当超过目标值就把这个监控的任务暂停。这样事发后限制其实治标不治本,终于客户提出了新的需求,不能停止任务只下载指定大小的文件并返回md5值由客户去校验正确性。

经过了一些尝试,解决了这个问题,记录过程如下文。

1、尝试使用 CURLOPT_MAXFILESIZE。

对php和libcurl的版本有版本要求,完全的事前处理,当发现目标大于设置时,直接返回了超过大小限制的错误而不去下载目标了,不符合要求。

2、使用curl下载过程的回调函数。

参考 http://php.net/manual/en/function.curl-setopt-array.php ,最终使用了CURLOPT_WRITEFUNCTION参数设置了on_curl_write,该函数将会1s中被回调1次。

  1. $ch = curl_init(); 
  2. $options = array(CURLOPT_URL    => 'http://www.php.net/', 
  3. CURLOPT_HEADER    => false, 
  4. CURLOPT_HEADERFUNCTION  => 'on_curl_header', 
  5. CURLOPT_WRITEFUNCTION  => 'on_curl_write' 
  6. ); 

最终我的实现片段:

  1. function on_curl_write($ch, $data) 
  2. { 
  3.   $pid = getmypid(); 
  4.   $downloadSizeRecorder = DownloadSizeRecorder::getInstance($pid); 
  5.   $bytes = strlen($data); 
  6.   $downloadSizeRecorder->downloadData .= $data; 
  7.   $downloadSizeRecorder->downloadedFileSize += $bytes; 
  8. //  error_log(' on_curl_write '.$downloadSizeRecorder->downloadedFileSize." > {$downloadSizeRecorder->maxSize} \n", 3, '/tmp/hyb.log'); 
  9.   //确保已经下载的内容略大于最大限制 
  10.   if (($downloadSizeRecorder->downloadedFileSize - $bytes) > $downloadSizeRecorder->maxSize) { 
  11.     return false; 
  12.   } 
  13.   return $bytes; //这个不正确的返回,将会报错,中断下载 "errno":23,"errmsg":"Failed writing body (0 != 16384)" 
  14. } 

DownloadSizeRecorder是一个单例模式的类,curl下载时记录大小,实现返回下载内容的md5等。

  1. class DownloadSizeRecorder 
  2. { 
  3.   const ERROR_FAILED_WRITING = 23; //Failed writing body 
  4.   public $downloadedFileSize; 
  5.   public $maxSize; 
  6.   public $pid; 
  7.   public $hasOverMaxSize; 
  8.   public $fileFullName; 
  9.   public $downloadData; 
  10.   private static $selfInstanceList = array(); 
  11.   public static function getInstance($pid) 
  12.   { 
  13.     if(!isset(self::$selfInstanceList[$pid])){ 
  14.       self::$selfInstanceList[$pid] = new self($pid); 
  15.     } 
  16.     return self::$selfInstanceList[$pid]; 
  17.   } 
  18.   private function __construct($pid) 
  19.   { 
  20.     $this->pid = $pid; 
  21.     $this->downloadedFileSize = 0; 
  22.     $this->fileFullName = ''; 
  23.     $this->hasOverMaxSize = false; 
  24.     $this->downloadData = ''; 
  25.   } 
  26.   /** 
  27.    * 保存文件 
  28.    */ 
  29.   public function saveMaxSizeData2File(){ 
  30.     if(emptyempty($resp_data)){ 
  31.       $resp_data = $this->downloadData; 
  32.     } 
  33.     $fileFullName = '/tmp/http_'.$this->pid.'_'.time()."_{$this->maxSize}.download"; 
  34.     if($resp_data && strlen($resp_data)>0) 
  35.     { 
  36.       list($headerOnly, $bodyOnly) = explode("\r\n\r\n", $resp_data, 2); 
  37.       $saveDataLenth = ($this->downloadedFileSize < $this->maxSize) ? $this->downloadedFileSize : $this->maxSize; 
  38.       $needSaveData = substr($bodyOnly, 0, $saveDataLenth); 
  39.       if(emptyempty($needSaveData)){ 
  40.         return; 
  41.       } 
  42.       file_put_contents($fileFullName, $needSaveData); 
  43.       if(file_exists($fileFullName)){ 
  44.         $this->fileFullName = $fileFullName; 
  45.       } 
  46.     } 
  47.   } 
  48.   /** 
  49.    * 返回文件的md5 
  50.    * @return string 
  51.    */ 
  52.   public function returnFileMd5(){ 
  53.     $md5 = ''; 
  54.     if(file_exists($this->fileFullName)){ 
  55.       $md5 = md5_file($this->fileFullName); 
  56.     } 
  57.     return $md5; 
  58.   } 
  59.   /** 
  60.    * 返回已下载的size 
  61.    * @return int 
  62.    */ 
  63.   public function returnSize(){ 
  64.     return ($this->downloadedFileSize < $this->maxSize) ? $this->downloadedFileSize : $this->maxSize; 
  65.   } 
  66.   /** 
  67.    * 删除下载的文件 
  68.    */ 
  69.   public function deleteFile(){ 
  70.     if(file_exists($this->fileFullName)){ 
  71.       unlink($this->fileFullName); 
  72.     } 
  73.   } 
  74. } 

curl请求的代码实例中,实现限制下载大小

  1. …… 
  2. curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'on_curl_write');//设置回调函数 
  3. …… 
  4. $pid = getmypid(); 
  5. $downloadSizeRecorder = DownloadSizeRecorder::getInstance($pid); 
  6. $downloadSizeRecorder->maxSize = $size_limit; 
  7. …… 
  8. //发起curl请求 
  9. $response = curl_exec($ch); 
  10. …… 
  11. //保存文件,返回md5 
  12. $downloadSizeRecorder->saveMaxSizeData2File(); //保存 
  13. $downloadFileMd5 = $downloadSizeRecorder->returnFileMd5(); 
  14. $downloadedfile_size = $downloadSizeRecorder->returnSize(); 
  15. $downloadSizeRecorder->deleteFile(); 

到这里,踩了一个坑。增加了on_curl_write后,$response会返回true,导致后面取返回内容的时候异常。好在已经实时限制了下载的大小,用downloadData来记录了已经下载的内容,直接可以使用。

  1. if($response === true){ 
  2.   $response = $downloadSizeRecorder->downloadData; 
  3. } 

Tags: curl下载 php下载

分享到: