首页 新闻 会员 周边

php stream_context_create 函数设置访问超时问题

-1
悬赏园豆:20 [已解决问题] 解决于 2012-04-17 18:15

小白求助:

    使用 stream_context_create 方法设置 file_get_contents 函数超时的时候:

<?php
$ctx = stream_context_create(array(
    'http' => array(
        'timeout' => 1  // 超时
        )
    )
);

$i = microTime();
$size = file_get_contents("http://****{图*片*地*址}******.jpg",0,$ctx);
$e = microTime() - $i;
echo $e;

// 获得微秒时间戳
function microTime()
{
        list($usec, $sec) = explode(" ",microtime());
        return ((float)$usec + (float)$sec);
}

?>

    发现 当 timeout 设置为 1 时,只有当访问时长 $e 超过2s时才会报错:

Warning: file_get_contents(********): failed to open stream: HTTP request failed!  in /usr/home/*******/test.php

    同理 当 timeout 设置为 2 时,访问时长 $e 的值可能为 2.4s、2.6s等(小于3s,超过3s时报错)。

 

请问:

    1、 如何设置 file_get_contents() 函数的超时时间?

    2、以上描述的问题是什么情况?

    3、能否设置 file_get_contents() 函数的毫秒级超时,如果能,如何实现?

问题补充:

木有人知道吗?

魇靥的主页 魇靥 | 初学一级 | 园豆:195
提问于:2012-04-16 18:35
< >
分享
最佳答案
0

不懂 帮忙顶下

收获园豆:20
顶之峰 | 菜鸟二级 |园豆:222 | 2012-04-17 18:13
其他回答(1)
0

没人回答啊

魇靥 | 园豆:195 (初学一级) | 2012-04-17 18:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册