0

The code below(learned from Save image from url with curl PHP) works fine when try to grap image from internet. But when come to the url below, I only got a "test.jpg" which is actually a 404 error page("test.jpg" could be opened by notepad). PS: And i could open the url with browser and could see the image. Thanks to Mike,Problem solved and code updated.

$url = 'https://spthumbnails.5min.com/10368406/518420256_c_570_411.jpg';
$reffer="http://www.sohu.com";
$user_agent="Baiduspider+(+http://www.baidu.com/search/spider.htm)";
$saveto="test.jpg";
grab_image($url,$saveto);

function grab_image($url,$saveto,$reffer,$user_agent){
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    curl_setopt($ch,CURLOPT_REFERER,$reffer);
    curl_setopt($ch,CURLOPT_USERAGENT,$user_agent);
    $raw=curl_exec($ch);
    curl_close ($ch);
    $fp = fopen($saveto,'w');
    fwrite($fp, $raw);
    fclose($fp);
}
Community
  • 1
  • 1
weblen
  • 149
  • 1
  • 2
  • 9
  • 2
    Try setting a referrer and user agent in your curl request. Some sites block requests that don't contain them. – Mike Dec 31 '14 at 00:58

1 Answers1

0

Thanks to Mike. This site do need "CURLOPT_REFERER" option(which i ignored) to grap the image. And I also add the useragent option to make sure it work on other situation.

weblen
  • 149
  • 1
  • 2
  • 9