[英]Header response “403 Forbidden” with cURL, file_get_contents() and fopen()
希望您能對我有所幫助。 該網站當前在線,可以正常訪問,但我似乎不明白為什么它無法正常工作。
file_get_contents()
和fopen()
返回以下錯誤:
PHP Warning: file_get_contents(http://www.hackforums.net): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /host/Users/Phizo/Desktop/stalker.php on line 29
現在我剛開始使用cURL,因為我想嘗試解決這個403問題,也沒有運氣。
$handle = curl_init();
$file = fopen('source.txt', 'w');
curl_setopt($handle, CURLOPT_URL, 'http://www.hackforums.net/');
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_AUTOREFERER, true);
curl_setopt($handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1');
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_FILE, $file);
curl_exec($handle);
print_r(curl_getinfo($handle));
curl_close($handle);
fclose($file);
輸出以下錯誤:
Fatal error: Maximum execution time of 30 seconds exceeded in D:\Hosting\6514439\html\zeonsglobal\admin\press_uploads\stalker.php on line 29
此代碼對我有用,您無需執行自定義請求即可實現所需的功能。
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, 'http://www.hackforums.net/');
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1');
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($handle);
var_dump($result);
curl_close($handle);
您收到403的可能原因是因為fopen正在傳遞用戶代理。 如果從curl請求中刪除用戶代理,您還將收到403錯誤。
我添加了curl選項CURLOPT_RETURNTRANSFER
因此當您調用curl_exec()
時,它以字符串形式返回響應。
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.