[PHP] file_get_contents: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
之前寫了一個小程式用 file_get_contents 去爬別人家網站的資料,但最近突然無法使用,噴出錯誤訊息:
經查詢是因為沒有指定 user-agent,這樣一來就沒辦法簡單用 file_get_contents 去撈資料,只好改成 curl,程式碼分別如下:
原來的程式碼:
Warning: file_get_contents(<url>): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in <Your Program> on line <Error Line>
經查詢是因為沒有指定 user-agent,這樣一來就沒辦法簡單用 file_get_contents 去撈資料,只好改成 curl,程式碼分別如下:
原來的程式碼:
$contents = file_get_contents($url);改版後的程式碼:
$curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, $url); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'); curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false); $contents = curl_exec($curl_handle); curl_close($curl_handle);
留言
張貼留言