網站首頁 編程語言 正文
使用 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
相關推薦
- 2022-07-07 基于Python制作一個文件解壓縮工具_python
- 2022-07-18 MVC與MVVM的區別與理解
- 2022-12-09 使用adb命令從電腦傳文件到手機(傳文件)_Android
- 2022-08-30 關于Flask高級_WTForms介紹和基本使用
- 2022-05-01 Python的裝飾器詳情介紹_python
- 2023-02-01 Python中使用zip函數的七重境界解析_python
- 2022-05-09 Redis擊穿穿透雪崩產生原因分析及解決思路面試_Redis
- 2022-10-02 C++?OpenCV實戰之手寫數字識別_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支