如何在PHP中设置get_file_contents的时间限制?

Jag*_*ira 12 php

有时get_file_contents需要太长时间并挂起整个脚本.有没有办法在get_file_contents上设置超时限制,而不修改脚本的最大执行时间?

编辑:

它花了很长时间,因为文件不存在.我收到"无法打开流:HTTP请求失败!" 错误.但它需要永远.

Pek*_*ica 36

通过使用timeout选项创建上下文,似乎可以在PHP> 5.2.1中实现.

手册页中略有修改的示例:

<?php

$opts = array('http' =>
    array(
        'method'  => 'GET',
        'timeout' => 120 
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.com/get.php', false, $context);

?>
Run Code Online (Sandbox Code Playgroud)