php stream_context_create指定编码,php stream_context_create 函数设置访问超时问题

小白求助:

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

$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() 函数的毫秒级超时,如果能,如何实现?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值