I am using file_get_contents
to get a headers of an external page to determine if the external page is online like so:
$URL = "http://page.location/";
$Context = stream_context_create(array(
'http' => array(
'method' => 'GET',
)
));
file_get_contents($URL, false, $Context);
$ResponseHeaders = $http_response_header;
$header = substr($ResponseHeaders[0], 9, 3);
if($header[0] == "5" || $header[0] == "4"){
//do stuff
}
This is working well except when the page is taking too long to respond.
How do I set a timeout?
Will file_get_headers
return FALSE if it has not completed yet and will PHP move to the next line if it has not completed the file_get_contents
request?
Here is an example of how can you set the timeout for this function:
<?php
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 1
)
)
);
file_get_contents("http://example.com/", 0, $ctx);
?>