duanmianzhou5353 2016-10-15 14:09
浏览 39
已采纳

试图用PHP下载远程图像

I've trying to download remote images(from more than one site) but any of these codes worked

allow_url_fopen is on but these code will return 0 bytes

$url = 'http://example.com/image.php';
$img = 'teste.jpg';
file_put_contents($img, file_get_contents($url));

or

copy('http://example.com/image.php', 'teste.jpg');

When I use curl(that is enabled on host) this error appears:

Error: The requested resource could not be loaded. libcurl returned the error: Empty reply from server

curl code :

$ch = curl_init('http://example.com/image.php');
$fp = fopen('teste,jpg, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Edit: Tried following curl opt too:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_FILE, $lfile);

site is hosted by 000webhost, and after trying my whole day with differents codes I had to use a proxy to access my site because it was blocking me

Edit 2:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"h.mhcdn.net/store/manga/10738/thumb_cover.jpg?v=1476422230");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
//die("here1");
$data = curl_exec($ch);
die("here2");
file_put_contents("test.jpg", $data);
die("here3");
//curl_setopt($ch, CURLOPT_FILE, "test.jpg");

Tried code above but it wont reach "die("here2");"(page wont load) only the die("here1");

Edit 3:

$url = "http://h.mhcdn.net/store/manga/10738/thumb_cover.jpg?v=1476422230";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
        //die("here1");
        $data = curl_exec($ch);
        file_put_contents("local_file.jpg", $data);
        die("here2");

Can't reach here2, only here1

Edit 4: here's the problem: port 80 has been blocked for outgoing connections at 000web : https://www.habschned.com/000webhost-com-blocking-port-80-for-outbound-traffic-no-more-curl/

  • 写回答

1条回答 默认 最新

  • dongma7725 2016-10-15 14:16
    关注

    I guess it's pretty obvious, but did you try to open the URL in your browser? Can you see the image?

    If the answer to the above is YES (most likely), most likely the remote party is blocking scripts from fetching the resource. The solution is to use cURL emulating as best as possible the behaviour of a regular browser, in terms of HTTP headers.

    I would start from the most obvious ones which are CURLOPT_USERAGENT and CURLOPT_USERAGENT, set them at the values they expect and you should be fine. More complex access controls involve cookies and sessions, which is more complicated because you would have to fetch the referring page, store the cookie, and send it while fetching the resource.

    UPDATE:

    While the above is true, it was not the problem the asker had, this simple script should work as-is:

    $url = "http://h.mhcdn.net/store/manga/10738/thumb_cover.jpg?v=1476422230";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
    $data = curl_exec($ch);
    file_put_contents("local_file.jpg", $data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度