php file_get_contents timeout,php get_headers timeout file_get_contents timeout 执行超时处理

1. get_headers() timeout超时处理

$urls = array(

'http://www.php-php.com/',

'http://www.163.com/',

'http://php.xlxz.org.la/'

);

function xlxzUrl($url) {

// 避免请求超时超过了PHP的执行时间

$executeTime = ini_get('max_execution_time');

ini_set('max_execution_time', 0);

$headers = @get_headers($url);

ini_set('max_execution_time', $executeTime);

if ($headers) {

$head = explode(' ', $headers[0]);

if (!empty($head[1]) && intval($head[1]) < 400)

return true;

}

}

foreach ($urls as $url) {

if ( xlxzUrl( $url ) )

echo $url . '有效' . '
';

else

echo $url . '无效' . '
';

}

2. get_headers() timeout 超时处理

使用 stream_context_set_default() 函数 ( >php5.3 )

stream_context_set_default(array(

'http' => array(

'timeout' => 1 //设置一个超时时间,timeout单位为秒

)

)

);

$headers = @get_headers( 'no.xlxz.org' );

3.  file_get_contents() timeout 超时处理

$stream = stream_context_create(array(

'http' => array(

'timeout' => 1 //设置一个超时时间,timeout单位为秒

)

)

);

file_get_contents("http://xlxz.org/", 0, $strem);

4. get_headers() timeout 超时处理使用CURL来达到相同功能

function get_headers_curl($url, $time = 15 )

{

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_HEADER, true);

curl_setopt($ch, CURLOPT_TIMEOUT, $time );

curl_setopt($ch, CURLOPT_NOBODY, true);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$xlxz = curl_exec($ch);

curl_close( $ch );

$xlxz = explode( "\n", $xlxz );

return $xlxz;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值