How can I set time limit on get_file_contents in PHP?
At times the get_file_contents takes too long and that hangs the entire script. Is there any way of setting a time out limit on get_file_contents, without modifying the maximum execution time of the script?
Edit:
Its taking long because the 开发者_JAVA技巧file does not exist. I am getting "failed to open stream: HTTP request failed!" error. But it takes forever.
Seems to be possible in PHP > 5.2.1 by creating a context with the timeout option.
Slightly amended example from the manual page:
<?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);
?>
you count use ini_set
and set "default_socket_timeout"
before using file_get_contents
and then restore the old value after it - if would affect some other parts of your code...
精彩评论