`
txf2004
  • 浏览: 6874451 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

【分享】完美解决failed to open stream: HTTP request failed!

 
阅读更多

这个是我以前在工作中遇到过的问题和在网上查到的解决方案拿出来分享一下。这个主要是file_get_contents函数抓取网页超时的问题。【高手就飘过吧,呵呵】


完美解决failed to open stream: HTTP request failed!


时间:2010-08-26 16:57 来源:PHP100中文网 作者:网络 点击: 8 次 【字号:大 中 小】
当使用php5自带的file_get_contents方法来获取远程文件的时候,有时候会出现file_get_contents(): failed to open stream: HTTP request failed!这个警告信息。google或者baidu一下,好多这样的问题,解决的方法都是修改php.ini,把allow_url_fopen给启用,


当使用php5自带的file_get_contents方法来获取远程文件的时候,有时候会出现file_get_contents(): failed to open stream: HTTP request failed!这个警告信息。
google 或者baidu一下,好多这样的问题,解决的方法都是修改php.ini,把allow_url_fopen给启用,改成allow_url_fopen = On这样做可以解决某些人的问题,有人说在php.ini中,有这样两个选项:allow_url_fopen=on(表示可以通过url打开远程文件),user_agent="PHP"(表示通过哪种脚本访问网络,默认前面有个 " ; "去掉即可。)重启服务器。
但是有些还是会有这个警告信息,想用完美的解决还差一步,还得设置php.ini里面的user_agent,php默认的user_agent是PHP,我们把它改成 Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)来模拟浏览器就可以了
user_agent=”Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)”
在工作中遇到这个问题,后完美解决,故分享给大家。
我批量抓取chemblink的结构式发现循环后有部分图片无法显示,而远程文件是存在的。
抓取远程文件的时候出现类似Warning: readfile(http://www.php100.com/logo.gif) [function.readfile]: failed to open stream: HTTP request failed! 这样的警告信息,我使用的是
ob_start();
readfile("http://www.php100.com/logo.gif");
$img = ob_get_contents();
ob_end_clean();

这样在运行中会时不时的出现上述错误,我也换过file_get_contents等其他函数都没用用,在网上查阅后发现用CURL方法抓取不会出错
$url = "http://www.php100.com/logo.gif";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
$img = curl_exec($ch);

分享到:
评论

相关推荐

    php错误提示failed to open stream: HTTP request failed!的完美解决方法

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

    详解PHP内置访问资源的超时时间 time_out file_get_contents read_file

    [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 500 Read timed outin D:\website\extra.php on line 65”我在程序的开始已经有set_time_limit(0);了啊,那上面的错误会是因...

    HTTP 错误 403.1禁止访问:禁止执行访问的完美解决方法

    解决方法该页无法显示 您试图从目录中执行 CGI、ISAPI 或其他可执行程序,但该目录不允许执行程序。 请尝试以下操作: 如果您认为该目录应该允许执行访问权限,请与网站管理员联系。 HTTP 错误 403.1 – 禁止访问:...

    php 获取远程网页内容的函数

    <?php $curDomain = $_SERVER[‘HTTP_HOST’]; $strHTML = file_get_contents(...; echo $strHTML ?> 早就在网上看到说file_get_contents不稳定,果然碰到了。...: failed to open stream: HTTP request

    计算机网络第六版答案

    14. If the two ISPs do not peer with each other, then when they send traffic to each other they have to send the traffic through a provider ISP (intermediary), to which they have to pay for carrying ...

    高清监控系统兼容所有网络摄像机

    Open update file failed please check if the update file exist=打开文件失败,请检查升级文件是否存在! Open Version file fail=打开版本文件失败 Download:=下载进度: Restart the device, are you sure?=设备...

    eac3to V3.17

    * "eac3to some.mpls" now also works if the stream files aren't there, anymore * fixed: number of subtitles was not appended to demuxed subtitles' file name * fixed: dialnorm removal (for Nero decoder)...

    esp8266 mp3

    //Open a connection to a webserver and request an URL. Yes, this possibly is one of the worst ways to do this, //but RAM is at a premium here, and this works for most of the cases. int ICACHE_FLASH_...

    php.ini-development

    request_order ; Default Value: None ; Development Value: "GP" ; Production Value: "GP" ; session.gc_divisor ; Default Value: 100 ; Development Value: 1000 ; Production Value: 1000 ; session.hash_...

    servlet2.4doc

    Called by the server (via the service method) to allow a servlet to handle a OPTIONS request. doPost(HttpServletRequest, HttpServletResponse) - Method in class javax.servlet.http.HttpServlet Called ...

    一个进程池的服务器程序

    printf("open file %s failed\n", filepath); close(connfd); exit(-1); } while((count = fread(content, 1, sizeof(content), fp)) > 0) { //printf("%s", content); if (write(connfd, content, count) !=...

    Senfore_DragDrop_v4.1

    * Virtual File Stream formats can only be pasted from the clipboard with live data (i.e. FlushClipboard/OleFlushClipboard hasn't been called on the data source). This problem affects ...

    JDK_1_6 API

    javax.management.openmbean 提供开放数据类型和 Open MBean 描述符类。 javax.management.relation 提供 Relation Service 的定义。 javax.management.remote 对 JMX MBean 服务器进行远程访问使用的接口。 javax....

Global site tag (gtag.js) - Google Analytics