PHP问题 —— failed to open stream: HTTP request faile

简介:

1.1  failed to open stream: HTTP request failed!

1.1.1  现象

 当使用php5自带的file_get_contents方法来获取远程文件的时候,有时候会出现file_get_contents(): failed to open stream: HTTP request failed!这个警告信息。

1.1.2  原因

在用file_get_contents函数采集网站时,有时会遇到明明用浏览器可以看,但就是采不到内容的问题。这很有可能是服务器上做了设置,根据 User_agent判断是否为正常的浏览器请求,默认PHP的file_get_contents函数是不发送ua的,如果要采集这样的网站,我们就要 让PHP模拟浏览器发送UA,这样对方的服务器就会以为我们是用浏览器是浏览,而返回正常的内容。

1.1.3  解决

设置php.ini里面的user_agent,php默认的user_agent是PHP,我们把它改成Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)来模拟浏览器就可以了

将user_agent=“PHP”

修改为 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)"






本文转自 tywali 51CTO博客,原文链接:http://blog.51cto.com/lancelot/1772955,如需转载请自行联系原作者
目录
相关文章
|
2月前
|
机器学习/深度学习 前端开发 JavaScript
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
43 0
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
|
消息中间件 人工智能 移动开发
阿里云Rocket MQ PHP Http SDK发送消息示例Demo
消息队列 RocketMQ 版支持 RESTful 风格的 HTTP 协议通信,并提供了以下 7 种语言的 SDK,下面以最新的PHP Http SDK为范例介绍RocketMQ消息的发送。
2668 0
阿里云Rocket MQ PHP Http SDK发送消息示例Demo
|
SQL Linux
Cannot connect to discovery server for announce: Announcement failed for http://hadoop102:8881
linux下启动Presto报错:Cannot connect to discovery server for announce: Announcement failed for http://hadoop102:8881
Cannot connect to discovery server for announce: Announcement failed for http://hadoop102:8881
|
8月前
|
Web App开发 JavaScript 前端开发
解决DevTools failed to load SourceMap Could not load content for .js.map HTTP error code 404 问题
解决DevTools failed to load SourceMap Could not load content for .js.map HTTP error code 404 问题
228 0
|
6月前
|
Java 应用服务中间件 网络安全
关于 Commerce 启动时遇到的错误消息 failed to initialize connector HTTP 9001
关于 Commerce 启动时遇到的错误消息 failed to initialize connector HTTP 9001
62 0
|
6月前
|
应用服务中间件 开发工具 nginx
【git】解决:error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413
【git】解决:error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413
165 0
|
7月前
|
应用服务中间件 PHP nginx
PHP ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)
PHP ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)
54 1
|
JSON 前端开发 PHP
在php里发送请求,php发送http请求的几种方法
还在等什么,快来一起讨论关注吧,公众号【八点半技术站】,欢迎加入社群
|
Apache Android开发
解决Failed resolution of: Lorg/apache/http/client/methods/HttpEntityEnclosingRequestBase的方案
解决Failed resolution of: Lorg/apache/http/client/methods/HttpEntityEnclosingRequestBase的方案
241 0
解决Failed resolution of: Lorg/apache/http/client/methods/HttpEntityEnclosingRequestBase的方案
|
12月前
|
Java 应用服务中间件 Spring
Failed to start connector [Connector[HTTP/1.1-8080]]
1、解决Web server failed to start. Port 8080 was already in use 2、SpringBoot启动报错:"Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled." 3、Failed to start end point associated with ProtocolHandler [http-nio-8080] 4、Failed to start connecto
1710 0