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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理