linux - PHP uses curl to process file download url connections, and encounters some problems
習慣沉默
習慣沉默 2017-06-10 09:47:47
0
2
782
  1. The url connection is a download link for a .gz compressed file

  2. Generally, the method of using curl to process data to return data will use

  3. But when it came to processing downloaded files, I was confused.

  4. Please give me some advice from the masters you have met

public static function curl($url, $params = false, $ispost = 0, $https = 0)
    {
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($https) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
        }
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                if (is_array($params)) {
                    $params = http_build_query($params);
                }
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }

        $response = curl_exec($ch);

        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }
習慣沉默
習慣沉默

reply all(2)
给我你的怀抱

Generally, the method of using curl to process data when returning data is to use
Just write the returned content using file_put_contents

file_put_contents(__DIR__.'/yourfilename.ext', $response);
女神的闺蜜爱上我

Do it like this

 //分离header与body  
    $header = '';  
    $body = '';  
    if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {  
        $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); //头信息size  
        $header = substr($response, 0, $headerSize);  
        $body = substr($response, $headerSize);  
    }  
   curl_close($ch);  

   //文件名  
    $arr = array(); $savePath = "/"; 
    if (preg_match('/filename="(.*?)"/', $header, $arr)) {  
  
        $file = date('Ym') . '/' . $arr[1];  
        $fullName = $savePath . '/' . $file;  
  
        //创建目录并设置权限  
        $basePath = dirname($fullName);  
        if (!file_exists($basePath)) {  
            @mkdir($basePath, 0777, true);  
            @chmod($basePath, 0777);  
        }  
       
        
        file_put_contents($fullName, $body);
    }  
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!