GVKun编程网logo

PHP file_get_contents忽略超时?(php file get contents)

12

对于PHPfile_get_contents忽略超时?感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解phpfilegetcontents,并且为您提供关于curl和file_get_con

对于PHP file_get_contents忽略超时?感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解php file get contents,并且为您提供关于curl 和 file_get_contents 抓取网页乱码的解决之道 filegetcontents超时 js file get contents wp file get contents、file_get_contents PHP file_get_contents 函数超时的几种解决方法、file_get_contents PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析、file_get_contents PHP下通过file_get_contents的代理使用方法的宝贵知识。

本文目录一览:

PHP file_get_contents忽略超时?(php file get contents)

PHP file_get_contents忽略超时?(php file get contents)

$url = 'http://a.url/i-kNow-is-down';

//ini_set('default_socket_timeout',5);

$ctx = stream_context_create(array(
    'http' => array(
        'timeout' => 5,'ignore_errors' => true
        )
    )
);

$start = microtime(true);
$content = @file_get_contents($url,false,$ctx);
$end = microtime(true);
echo $end - $start,"\n";

我得到的回复一般是21.232 segs,不应该是大约五秒钟

取消注释ini_set行根本没有帮助.

您正在使用socket_create_context设置读取超时.如果您尝试访问的页面不存在,则服务器将允许您连接并给您一个404.但是,如果该站点不存在(将不会解析或没有Web服务器在后面),那么file_get_contents( )将忽略读取超时,因为它甚至没有超时连接到它.

我不认为你可以在file_get_contents中设置连接超时.我最近重写了一些代码来使用fsockopen(),正是因为它允许你指定connect timeout

$connTimeout = 30 ;
$fp = fsockopen($hostname,$port,$errno,$errstr,$connTimeout);

那么去fsockopen的方法需要你再循环一遍fread(),稍微复制你的代码.但是,当您使用stream_get_Meta_data()读取时,会检测到读取超时,

http://php.net/stream_get_meta_data

curl 和 file_get_contents 抓取网页乱码的解决之道 filegetcontents超时 js file get contents wp file get contents

curl 和 file_get_contents 抓取网页乱码的解决之道 filegetcontents超时 js file get contents wp file get contents

    今天用 curl_init 函数抓取搜狐的网页时,发现采集的网页时乱码,经过分析发现原来是服务器开启了gzip压缩功能。只要往函数 curl_setopt 添加多个选项 curlopt_encoding 解析 gzip 就可以正确解码了。
    还有如果抓取的网页时 gbk 编码,但是脚本确是 utf-8 编码,还得把抓取的网页再用函数 mb_convert_encoding 转换下。
    $tmp = sys_get_temp_dir();
    $cookiedump = tempnam($tmp, ''cookies'');
    $url = ''http://tv.sohu.com'';
    $ch = curl_init();
    curl_setopt ($ch, curlopt_url, $url);
    curl_setopt ($ch, curlopt_header, 1);// 显示返回的header区域内容
    curl_setopt ($ch, curlopt_followlocation, 1); // 使用自动跳转
    curl_setopt ($ch, curlopt_timeout, 10);// 设置超时限制
    curl_setopt ($ch, curlopt_returntransfer, 1); // 获取的信息以文件流的形式返回
    curl_setopt ($ch, curlopt_connecttimeout,10);// 链接超时限制
    curl_setopt ($ch, curlopt_httpheader,array(''accept-encoding: gzip, deflate''));//设置 http 头信息
    curl_setopt ($ch, curlopt_encoding, ''gzip,deflate'');//添加 gzip 解码的选项,即使网页没启用 gzip 也没关系
    curl_setopt ($ch, curlopt_cookiejar, $cookiedump);  // 存放cookie信息的文件名称
    $content = curl_exec($ch);
    // 把抓取的网页由 gbk 转换成 utf-8 
    $content = mb_convert_encoding($content,"utf-8","gbk");
?>
    $url = ''http://tv.sohu.com'';
    // 只要添加 compress.zlib 选项,即使服务器启用了gzip 压缩功能,就能够解码了
    $content = file_get_contents("compress.zlib://".$url);
    // 把抓取的网页由 gbk 转换成 utf-8 
    $content = mb_convert_encoding($content,"utf-8","gbk");
?>
原文:http://woqilin.blogspot.com/2014/05/curl-filegetcontents.html

以上就介绍了curl 和 file_get_contents 抓取网页乱码的解决之道,包括了file_get_contents方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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

file_get_contents 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(
‘method''=>”GET”,
‘timeout''=>60,
)
);
$context = stream_context_create($opts);
$html =file_get_contents(''http://www.example.com'', false, $context);
fpassthru($fp);


二、一次有延时的话那就多试几次
有时候失败是因为网络等因素造成,没有解决办法,但是可以修改程序,失败时重试几次,仍然失败就放弃,因为file_get_contents()如果失败将返回 FALSE,所以可以下面这样编写代码:

复制代码 代码如下:


$cnt=0;
while($cnt

以上就介绍了file_get_contents PHP file_get_contents 函数超时的几种解决方法,包括了file_get_contents方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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

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

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

file_get_contents 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,)
)
);
$result = file_get_contents($url, False, $ctx);
echo $result;
?>


另外一种 curl 的方式使用代理的方法:

复制代码 代码如下:

立即学习“PHP免费学习笔记(深入)”;


function postPage($url)
{
$response = "";
$rd=rand(1,4);
$proxy=''http://221.214.27.253:808'';
if($rd==2) $proxy=''http://222.77.14.56:8088'';
if($rd==3) $proxy=''http://202.98.123.126:8080'';
if($rd==4) $proxy=''http://60.14.97.38:8080'';
if($url != "") {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
$response = curl_exec($ch);
if(curl_errno($ch)) $response = "";
curl_close($ch);
}
return $response;
}


用file_get_contents解决ajax垮域问题
在ajax运用中有时候会垮域调用文件,而浏览器为了安全会默认给这种操作提出警告,甚至直接阻止。如果是IE会弹出一个警告窗口,询问你是否继续操作,只有你同意了IE才会调用垮域的文件。而其它浏览器,如火狐、Opera默认设置下则会直接提示错误,阻止调用外域文件。这会给用户不好的操作体验,如果想通过用户修改浏览器的安全设置来解决这个问题是不现实的,最好是在服务器端解决。
在服务器端可以使用一个同域的文件做为代理文件,这个代理文件将获得外域文件的内容,然后再传递给ajax。这样ajax就不是调用外域文件,而是调用同域的这个代理文件,安全问题也就解决了。
如果你的服务器端支持PHP的话,可以使用file_get_contents这个函数,看到它的名称就已经知道它有获得其它文件内容的功能了。它的详细用法可以参看PHP官方网站上的file_get_contents用法一页,下面是它的简单实例。

复制代码 代码如下:

立即学习“PHP免费学习笔记(深入)”;


$serverAddress = ''http://s.jb51.net'';
//获得外域文件内容
$randomNumber = file_get_contents($serverAddress);
//输出内容
echo $randomNumber;
?>

以上就介绍了file_get_contents PHP下通过file_get_contents的代理使用方法,包括了file_get_contents方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

关于PHP file_get_contents忽略超时?php file get contents的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于curl 和 file_get_contents 抓取网页乱码的解决之道 filegetcontents超时 js file get contents wp file get contents、file_get_contents PHP file_get_contents 函数超时的几种解决方法、file_get_contents PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析、file_get_contents PHP下通过file_get_contents的代理使用方法等相关内容,可以在本站寻找。

本文标签: