创建一个可以控制的资源句柄,通过控制资源句柄超时来控制file_get_contents这个方法的超时时间,使用起来很方便,也很简单。
$context = stream_context_create(array(
'http' => array(
'timeout' => 3000 //超时时间,单位为秒
)
));
// Fetch the URL's contents
$contents = file_get_contents('http://www.phprm.com', 0, $context);