日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

使用 file_get_contents 獲取網站信息報錯failed to open stream: HTTP request failed!

作者:沉、睡 更新時間: 2022-02-01 編程語言

使用 filegetcontents 獲取網站信息報錯failed to open stream: HTTP request failed!

當使用php5自帶的filegetcontents方法來獲取遠程文件的時候,有時候會出現filegetcontents(): failed to open stream: HTTP request failed!這個警告信息。

google或者baidu一下,好多這樣的問題,解決的方法都是修改php.ini,把allowurlfopen給啟用,改成 allowurlfopen = On

這樣做可以解決某些人的問題,有人說在php.ini中,有這樣兩個選項:allowurlfopen =on(表示可以通過url打開遠程文件),user_agent="PHP"(表示通過哪種腳本訪問網絡,默認前面有個 " ; " 去掉即可。)重啟服務器。

但是有些還是會有這個警告信息,想用完美的解決還差一步,還得設置php.ini里面的useragent,php默認的useragent是 PHP,我們把它改成Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)來模擬瀏覽器就可以了

user_agent="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; GreenBrowser)"

在用filegetcontents函數采集網站時,有時會遇到明明用瀏覽器可以看,但就是采不到內容的問題。這很有可能是服務器上做了設置,根據 Useragent判斷是否為正常的瀏覽器請求,默認PHP的filegetcontents函數是不發送ua的,如果要采集這樣的網站,我們就要 讓PHP模擬瀏覽器發送UA,這樣對方的服務器就會以為我們是用瀏覽器是瀏覽,而返回正常的內容。 發送UA方法很簡單,在使用fileget_contents函數前加上這一句:

ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; GreenBrowser)');
$url='http://www.baidu.com';
echo $flg=@file_get_contents($url);

其中Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; GreenBrowser)是 IE7 WindowsXP GreenBrowser環境下的UA,可根據需要自行修改成其他的,甚至可以偽裝蜘蛛。

Baiduspider+(+http://www.baidu.com/search/spider.htm)       百度蜘蛛User_agent
Sosospider+(+http://help.soso.com/webspider.htm)      SOSO蜘蛛User_agent
Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)      GOOGLE蜘蛛User_agent
Mozilla/5.0+(compatible;+Yahoo!+Slurp;+http://help.yahoo.com/help/us/ysearch/slurp)      Yahoo蜘蛛User_agent
Mozilla/5.0 (compatible; YoudaoBot/1.0; http://www.youdao.com/help/webmaster/spider/; )      有道蜘蛛User_agent

原文鏈接:https://blog.csdn.net/zz975896590/article/details/119991159

欄目分類
最近更新