首页 > 其他 > 详细

给file_get_contents函数设置超时时间

时间:2014-08-19 15:53:14      阅读:294      评论:0      收藏:0      [点我收藏+]
$opts = array(
‘http‘=>array(
‘method‘=>"GET",
‘timeout‘=>60,
)
);

$context = stream_context_create($opts);

$html =file_get_contents(‘http://www.example.com‘, false, $context);

 

这样,file_get_contents获取数据时,超出60秒将会自动退出.

还可以利用file_get_contents函数实现post功能

function Post($url, $post = null)
{
$context = array();

if (is_array($post)) {
ksort($post);

$context[‘http‘] = array (
‘timeout‘=>60,
‘method‘ => ‘POST‘,
‘content‘ => http_build_query($post, ‘‘, ‘&‘),
);
}

return file_get_contents($url, false, stream_context_create($context));
}

$data = array (
‘name‘ => ‘test‘,
‘email‘ => ‘test@gmail.com‘,
‘submit‘ => ‘submit‘,
);

echo Post(‘http://www.example.com‘, $data);

 

给file_get_contents函数设置超时时间,布布扣,bubuko.com

给file_get_contents函数设置超时时间

原文:http://www.cnblogs.com/yjken/p/3922130.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!