-1
下面的代碼(從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);
}
嘗試在curl請求中設置引薦來源和用戶代理。某些站點阻止不包含它們的請求。 – Mike