使用 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

使用 file_get_contents 获取网站信息报错failed to open stream: HTTP request failed!相关推荐

  1. file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

    使用file_get_contents()请求链接的时候报错: failed to open stream: HTTP request failed! 原因1:url参数值没有url编码 用 urle ...

  2. failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

    虽然fopen和file_get_contents都可以直接给url参数获取内容,有时会出现以下错误: failed to open stream: HTTP request failed! HTTP ...

  3. failed to open stream :HTTP request failed 解决方法

    failed to open stream :HTTP request failed 解决方法 参考文章: (1)failed to open stream :HTTP request failed ...

  4. springboot定时任务未登录情况下获取用户信息报错解决方案

    解决org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling ...

  5. proxy代理报错: Uncaught (in promise) Error: Request failed with status code 500

    POST http://localhost:8013/check/login 500 (Internal Server Error) createError.js:16 Uncaught (in pr ...

  6. 【hadoop】java 获取 yarn app 信息 报错 ConfiguredRMFailoverProxyProvider - Failing over to rm2

    文章目录 1.概述 1.概述 java 获取 yarn app 信息 报错 ConfiguredRMFailoverProxyProvider - Failing over to rm2 2022-0 ...

  7. 获取京东商品信息报错error pdos_captcha

    获取京东商品信息报错error pdos_captcha 2020.7.20 python 请求商品信息出现 {"error":"pdos_captcha"}. ...

  8. AndroidStudio编译报错org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:proces

    AndroidStudio编译报错 org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:proce ...

  9. IIS网站运行报错:无法识别的属性“targetFramework”。请注意属性名称区分大小写。

    报错信息如下: IIS网站运行报错:无法识别的属性"targetFramework".请注意属性名称区分大小写. 报错原因大概是:程序的.netframework版本为4.8,应用 ...

最新文章

  1. RibbitMQ 大数据分布式下的消息队列思
  2. 如何在迭代时从列表中删除项目?
  3. 设计模式中类之间的关系
  4. jquery 过滤html代码,jquery – 如何使指令使用过滤的HTML属性?
  5. AI+建筑战略再升级 旷视AIoT产品家族合体首秀
  6. Github | 深度神经网络(DNN)与生成式对抗网络(GAN)模型总览
  7. buntu下shell脚本运行异常:bash和…
  8. java 中的集合_JAVA中的集合
  9. WINDOWS下获得DLL所在目录的代码
  10. 码农小汪之理解Java注解。
  11. 手机上计算机怎么设置,电脑如何设置wifi让手机上网
  12. k8s初级班day2
  13. 【初学者】SDK的理解(客户端SDK和服务器端SDK)
  14. SF26 | 适用于商品+股指的枢轴点趋势策略
  15. KISSY基础篇乄KISSY之IO(1)
  16. diff = (diff*255).astype(“uint8“)
  17. 财务会计U8-出纳管理-日记账导入
  18. STM32F10××中英文手册
  19. vs2005+wince开发
  20. Linux下的动态监控进程

热门文章

  1. python计算两点间距离_python 计算方位角实例(根据两点的坐标计算)
  2. qt输出中文乱码处理(解决方法)
  3. DPDK 绑定网卡之后的解绑
  4. easyui datagrid加载本地数据和网络数据
  5. 学校计算机教室局域网的建立,校园局域网组建及配置.pdf
  6. c语言 统计数量用count_c语言中统计重复数字次数 c语言问题 统计不同数字的个数...
  7. vue引用electron_前端跨平台桌面开发技术:Electron 快速起步
  8. mysql存储日期 jsp_JSP+MySql的时间处理
  9. vue 保存时清空iuput_Vue 一键清空表单的实现方法
  10. 用 convert2rhel 工具将 CentOS 迁移至 RHEL