PHP curl下载图片时发送chunked

时间:2014-11-24 09:34:35

标签: php curl screen-scraping wget

为了下载图像我使用这个脚本:

<?php
    function download_image($url, $save_to) {
        if(file_exists($save_to))
            unlink($save_to);
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, false);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        $result = curl_exec($ch);
        curl_close($ch); 

        $fp = fopen($save_to, 'w');
        fwrite($fp, $result);
        fclose($fp);

        chmod($save_to, 0777);
    }

    $url = "http://flo.cubecdn.net/media/catalog/product/cache/1/image/400x400/9df78eab33525d08d6e5fb8d27136e95/4/0/4050266152131_1.jpg";
    download_image($url, "download.jpg");
?>

脚本完成下载后,将文件保存为download.jpg。但它不是jpeg文件。可能是什么问题?

编辑输出:

* Hostname was NOT found in DNS cache
*   Trying 195.244.32.93...
* Connected to flo.cubecdn.net (195.244.32.93) port 80 (#0)
> GET /media/catalog/product/cache/1/image/400x400/9df78eab33525d08d6e5fb8d27136e95/4/0/4050266152131_1.jpg HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1
Host: flo.cubecdn.net
Accept: */*
< HTTP/1.1 200 OK
* Server CCAcc (0.9.1/izm-s11) is not blacklisted
< Server: CCAcc (0.9.1/izm-s11)
< Date: Mon, 24 Nov 2014 10:03:35 GMT
< Content-Type: image/jpeg
< **Transfer-Encoding: chunked**
< Connection: keep-alive
< Last-Modified: Tue, 18 Nov 2014 05:47:03 GMT
< ETag: "546add57-50be"
< Content-Encoding: gzip
< Age: 0
< Expires: Wed, 24 Dec 2014 10:03:35 GMT
< X-US: HIT
< Cache-Control: max-age=2592000
< 
* Connection #0 to host flo.cubecdn.net left intact

编辑2: 我认为问题出在服务器发送图像时:

Transfer-Encoding: chunked

因为即使使用 wget

我也无法获得图片

0 个答案:

没有答案