file_get_contents获取不到网页内容的解决方法

代码如下:

<?php
$url = "http://jb51.net/index.html";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//在需要用户检测的网页里需要增加下面两行
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
?>

(0)

相关推荐

  • PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析

    后来,我通过跟踪发现,这类情况的出现,跟 PHP 的 file_get_contents() 函数有着密切的关系. 大.中型网站中,基于 HTTP 协议的 API 接口调用,是家常便饭.PHP 程序员们喜欢使用简单便捷的 file_get_contents("http://example.com/") 函数,来获取一个 URL 的返回内容,但是,如果 http://example.com/ 这个网站响应缓慢,file_get_contents() 就会一直卡在那儿,不会超时. 我们知道

  • 关于file_get_contents返回为空或函数不可用的解决方案

    如果你使用file_get_contents获取远程文件内容返回为空或提示该函数不可用,也许本文能帮到你! 使用file_get_contents和fopen必须空间开启allow_url_fopen.方法:编辑php.ini,设置allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件.如果你使用的是虚拟主机可以考虑用curl函数来代替.curl函数的使用示例: 复制代码 代码如下: $ch = curl_i

  • 探讨file_get_contents与curl效率及稳定性的分析

    做过好多抓取别家网站内容的产品,习惯了使用方便快捷的file_get_contents函数,但是总是会遇到获取失败的问题,尽管按照手册中的例子设置了超时,可多数时候不会奏效: 复制代码 代码如下: $config['context'] = stream_context_create(array('http' => array('method' => "GET",   'timeout' => 5//这个超时时间不稳定,经常不奏效   )  )); 这时候,看一下服务器

  • php中使用Curl、socket、file_get_contents三种方法POST提交数据

    抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简单,再就是需求也不大,所以没有学习使用curl.直到最近,要做一个网页小偷程序的时候才发现file_get_content已经完全不能满足需求了.我觉得,在读取远程内容的时候,file_get_content除了使用比curl便捷以外,其他都没有curl好. php中curl和file_get_content的一

  • PHP下通过file_get_contents的代理使用方法

    PHP使用file_get_contents的代理方法获取远程网页的代码. 复制代码 代码如下: <?php $url = "http://www.jb51.net/"; $ctx = stream_context_create(array( 'http' => array('timeout' => 5, 'proxy' => 'tcp://60.175.203.243:8080', 'request_fulluri' => True,) ) ); $re

  • 深入php函数file_get_contents超时处理的方法详解

    一.增加超时的时间限制 这里需要注意:set_time_limit只是设置你的PHP程序的超时时间,而不是file_get_contents函数读取URL的超时时间.真正的修改 file_get_contents延时可以用resource $context的timeout参数: 复制代码 代码如下: $opts = array(      'http'=>array(          'method'=>"GET",          'timeout'=>60, 

  • PHP file_get_contents 函数超时的几种解决方法

    这里就简单介绍两种: 一.增加超时的时间限制 这里需要注意:set_time_limit只是设置你的PHP程序的超时时间,而不是file_get_contents函数读取URL的超时时间. 我一开始以为set_time_limit也能影响到file_get_contents,后来经测试,是无效的.真正的修改file_get_contents延时可以用resource $context的timeout参数: 复制代码 代码如下: $opts = array( 'http'=>array( 'met

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

    提问我循环用file_get_contents抓取一堆url,但总是会在不到第100个URL的时候停下,提示我:"Warning: file_get_contents(URL) [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_l

  • 比file_get_contents稳定的curl_get_contents分享

    分享一个实际在用的函数: 复制代码 代码如下: /*比file_get_contents稳定的多!$timeout为超时时间,单位是秒,默认为1s.*/ function curl_get_contents($url,$timeout=1) { $curlHandle = curl_init(); curl_setopt( $curlHandle , CURLOPT_URL, $url ); curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1

  • php file_get_contents函数轻松采集html数据

    复制代码 代码如下: <?php //全国,判断条件是$REQUEST_URI是否含有html if (!strpos($_SERVER["REQUEST_URI"],".html")) { $page="http://qq.ip138.com/weather/"; $html = file_get_contents($page,'r'); $pattern="/<B>全国主要城市.县当天和未来五天天气趋势预报在线查询

随机推荐