0

I have a site that uses file_get_contents to access a geolocation API on every request. From time to time this remote API starts giving 504/502 errors, presumably due to problems at their end. The problem is that this causes my site to go down also - is there a way to create a fallback so that if the remote site starts timing out it will ignore it and load the page anyway?

This is my code:

$geoData = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']));
jamiemax
  • 179
  • 13
  • possible duplicate of [Good error handling with file\_get\_contents](http://stackoverflow.com/questions/3431169/good-error-handling-with-file-get-contents) – Jerodev Jul 10 '15 at 08:41
  • you could create a a context, set the timeout in the context then pass the context to file_get_contents - see the note on adding a context at http://php.net/manual/en/function.file-get-contents.php & http://php.net/manual/en/context.http.php – Rob G Jul 10 '15 at 08:41
  • possible duplicate of [Handling delays when retrieving files from remote server in PHP](http://stackoverflow.com/questions/1605063/handling-delays-when-retrieving-files-from-remote-server-in-php) EDIT: I see it's not timeouts but actual errors from the remote, so this is possibly not a dupe (but the end of the question does mention timeouts.... I am conflicted) – Amadan Jul 10 '15 at 08:42

2 Answers2

0

@$geoData = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']));

This will ignore any errors the line throws, and continue.

Keep in mind you will have to check if $geoData exists:

if($geoData){ // code }

Xeptix
  • 21
  • 4
  • if the site is timing out, this won't stop it timing out, it will just prevent the displaying of any errors caused – Rob G Jul 10 '15 at 08:47
  • My mistake - forgot that the website was timing out. However, I would assume the website would throw an error more often than it times out. In which case, this would actually work. – Xeptix Jul 10 '15 at 08:48
  • thanks, yeah i've got it to handle errors ok - the issue is when it times out – jamiemax Jul 10 '15 at 09:18
0

I understand, that this answer might be unwelcome, but here we go:

It is not a good idea at all to call the geolocation API on every request. This is nothing but a waste of resources - I think it is perfectly acceptable to treat the geoinfo of an IP as constant for the duration of a session.

My suggestion is to store the geoinfo in the session, and to fill it asynchronously - if you have no possibility for async PHP on the server, use an AJAX call, with a client-side error handler of "retry and ignore".

If you do not have a session infrastructure, you can use a local cache table, that again is asynchronously filled and has some sort of expiry mechanism.

This way, whenever the geoinfo site goes away, your app stays up, while ofcourse without the geoinfo component.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • yep you're right, that's a good idea. when i have time I'll sort it out but in the mean time I'll try the steps recommended in one of the questions mentioned above. – jamiemax Jul 10 '15 at 13:07