PHP File_Get_Contents Ignoring Timeout

PHP file_get_contents ignoring timeout?

You are setting the read timeout with socket_create_context. If the page you are trying to access doesn't exist then the server will let you connect and give you a 404. However, if the site doesn't exist (won't resolve or no web server behind it), then file_get_contents() will ignore read timeout because it hasn't even timed out connecting to it yet.

I don't think you can set the connection timeout in file_get_contents. I recently rewrote some code to use fsockopen() exactly because it lets you specify connect timeout

$connTimeout = 30 ;
$fp = fsockopen($hostname, $port, $errno, $errstr, $connTimeout);

Ofcourse going to fsockopen will require you to then fread() from it in a loop, compicating your code slightly. It does give you more control, however, on detecting read timeouts while reading from it using stream_get_meta_data()

http://php.net/stream_get_meta_data

file get content or fsockopen - timeout issue

The solution is to use PHP's cURL functions. The other question you linked to explains things properly, about the read timeouts vs. the connection timeouts, and so on, but neither of those are truly what you're looking for here. Even the connection timeout won't work, because the connection to testResponse.php is always successful; after that it's waiting, so what you need is an execution timeout. This is where cURL comes in handy.

So, testResponse.php doesn't need to be altered. In your main file, though, try the following code (this is tested and it works on my server):

$start = microtime(true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.mywebsite.com/testResponse.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$output = curl_exec($ch);
$errno = curl_errno($ch);
if ($errno > 0) {
if ($errno === 28) {
echo "Connection timed out.";
}
else {
echo "Error #" . $errno . ": " . curl_error($ch);
}
}
else {
echo $output;
}
$end = microtime(true);
echo "<br><br>" . ($end - $start);
curl_close($ch);

This sets the execution time of the cURL session, via the CURLOPT_TIMEOUT option you see on line 5. So, when the connection is timed out, $errno will equal 28, the code for cURL's operation timeout error. The rest of the error codes are listed in the cURL documentation, so you can expand the script above to act accordingly.

Finally, because of the CURLOPT_RETURNTRANSFER option that's set, curl_exec($ch) will be set to the content of the retrieved page if the session succeeds. Otherwise, it will equal false.

Hope this helps!

Edit: Removed the statement setting CURLOPT_HEADER. I also, for some reason, was under the impression that curl_exec($ch) set the value of $ch to the returned contents, forgetting that the contents are returned by curl_exec().

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);

PHP file_get_contents Returns 504 on Larger Files

You could try setting the default socket timeout as such:

ini_set('default_socket_timeout', 360);

Which would give the file_get_contents() 6 minutes to attempt its connection. Additionally, you could create a context array and pass it along to the file_get_contents() function which would make it so you didn't have to change the ini value ala:

$context = stream_context_create(array( 
'http' => array(
'timeout' => 360
)
)
);
file_get_contents("https://cdn.rawgit.com/everypolitician/everypolitician-data/65c4534/data/UK/Commons/ep-popolo-v1.0.json", 0, $context);

Hope this helps

Ignoring errors in file_get_contents HTTP wrapper?

If you don't want file_get_contents to report HTTP errors as PHP Warnings, then this is the clean way to do it, using a stream context (there is something specifically for that):

$context = stream_context_create(array(
'http' => array('ignore_errors' => true),
));

$result = file_get_contents('http://your/url', false, $context);

file_get_contents fails, but POST request is made successfully

After a few months I finally found the reason why this was happening.
The worst aspect of this is that I am familiar with error_reporting(E_ALL) and ini('error_notice',1) but I completely forgot about it. I did learn about var_dump($http_response_header) so there is that.
Honestly it was in the documentation and I should have found it sooner. Oh well.

I quote myself

With post requests that do not have session headers I do not have any problem, but once I needed these and tried sending them, I hit a roadblock

I was not familiar exactly with how session mechanics work in general, so i mostly ignored the documentation until I found it a few minutes ago. I refer to session_write_close as what I should have read before diving into sessions (or more specifically, attempting to open multiple sessions in a chain of requests (that I thought of as a single request)):

session data is locked to prevent concurrent writes only one script may operate on a session at any time

In my submit.php I open the session to access some variables. I basically forgot to close it before making a new request which itself attempted to modify session data. The fix was to use session_write_close() right before post_request().

Thank you to all who commented.

Does php's file_get_contents ignore file locking?

flock is relatively independent of the file operations, you can even use fopen on a locked file. You as developer are responsible for checking/using flock everywhere you require a lock.

But yes in that regard it's true that file_get_contents has no build-in way to acquire a read lock when reading the file. So the workaround would be the way to go.

file_put_contents allows you to get a lock for writing though.



Related Topics



Leave a reply



Submit