下面的代码(从Save image from url with curl PHP学到的)在试图从互联网上获取图像时工作得很好。但是当来到下面的url时,我只得到了一个"test.jpg“,它实际上是一个404错误页面(”test.jpg“可以通过记事本打开)。PS:我可以用浏览器打开网址,看到图像。感谢Mike,解决了问题并更新了代码。
$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);
}发布于 2014-12-31 01:19:44
多亏了迈克。这个站点确实需要"CURLOPT_REFERER“选项(我忽略了这个选项)来抓取图像。我还添加了useragent选项,以确保它在其他情况下工作。
https://stackoverflow.com/questions/27714793
复制相似问题