file_get_contents() Timeout in php
The default value for a timeout in file_get_contents() is 60 seconds.
You can change this value through the default_socket_timeout setting.
It's also doable in your code:
ini_set('default_socket_timeout', 2); // 2 seconds
if file_get_contents timeout, do something else
My Answer:
I saw this topic before
Does file_get_contents() have a timeout setting?
but it wasn't exactly what I want. I wanted to do something else if the timeout happens.
so I used this code:
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 5
)
)
);
$url = 'source URL';
$content = file_get_contents("$url", 0, $ctx);
if(!$content){
echo 'timeout happened';
}elseif (another condition) {
echo 'condition result'
}else {
echo 'something else'
}
PHP - Setting a file_get_contents timeout
Add a timeout
key inside the stream_context_array
$Context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'timeout' => 30, //<---- Here (That is in seconds)
)
));
Your Question....
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?
Yes , it will return FALSE along with the below warning message as shown.
A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection
failed because connected host has failed to respond.
How to set time limit for file_get_contents()?
You can use the HTTP context option
So the code would look like
$options = stream_context_create(array('http'=>
array(
'timeout' => 10 //10 seconds
)
));
echo file_get_contents('http://example.com/', false, $options);
php file_get_contents when does connection close?
file_get_contents
uses a regular stream context that you can even pass as 3rd parameter. You can either set a timeout option there.
$ctx = stream_context_create([
'http' ['timeout' => 10]
]);
$content = file_get_contents('http://...', FALSE, $ctx);
When you do not specify a http.timeout
context setting, it will default to the default_socket_timeout
php.ini setting (which is set to 60 seconds on most systems by default).
Timeout for file_get_contents don't works in PHP
for stream_context_create()
http://php.net/manual/en/function.stream-context-create.php it needs [ array $options [, array $params ]]
when you pass your $header
it doesnt appear that your building the array correctly. would something like this work?
public function myPost($content, $timeout = null)
{
$timeInit = new DateTime();
$this->method = 'POST';
$header = array();
$header['header'] = null;
$header['content'] = is_array($content) ? http_build_query($content) : $content;
$header['method'] = $this->method;
if ($timeout) {
$header['header']['timeout'] = $timeout;
}
$header['header']['Content-length'] . strlen($header['content']);
$headerContext = stream_context_create(array('http' => $header));
$contents = file_get_contents($this->url, false, $headerContext);
$this->responseHeader = $http_response_header;
$timeFinal = new DateTime();
$this->time = $timeInit->diff($timeFinal);
return $contents;
}
but a better way would be to use it like the example says, e.g.
$timeInit = new DateTime();
// all your defaults go here
$opts = array(
'http'=>array(
'method'=>"POST",
)
);
//this way, inside conditions if you want
$opts['http']['header'] = "Accept-language: en\r\n" . "Cookie: foo=bar\r\n";
$context = stream_context_create($opts);
Related Topics
How to Iterate Over the Results in a MySQLi Result Set
Instantiate a Class from a Variable in PHP
Change Product Prices Via a Hook in Woocommerce 3+
How to Use Guzzle to Send a Post Request in Json
Codeigniter Displays a Blank Page Instead of Error Messages
Get Country of Ip Address With PHP
How to Get Single Value from This Multi-dimensional PHP Array
Which One Is the Best Pdf-API For PHP
Search For a Key in an Array, Recursively
What Is the Best Collation to Use For MySQL With PHP
PHP Pdo: Get the Columns Name of a Table
PHP Output Showing Little Black Diamonds With a Question Mark
How to Insert HTML to PHP Domnode
Lamp: How to Create .Zip of Large Files For the User on the Fly, Without Disk/Cpu Thrashing