Adding a time out for empty results?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Adding a time out for empty results?

Post by Citizen »

I've created a google map that maps my users by IP address. I query hostip.info to get their geolocation but the problem is some of the results come back empty and hostip.info takes awhile to return the empty results.

I've done some research about sockets but I'm not sure how I would use it or even if I should.

Keep in mind that this code gives succesful results most of the time, but takes too long to time out if the results come back empty.

Anywho, here's my code inside my loop that provides the latitude and longitude for my map:

Code: Select all

$results[$i]=file_get_contents("http://www.hostip.info/api/get.html?ip=$ip&position=true");
                preg_match('/Latitude: (.*)/', $results[$i], $lat);
                preg_match('/Longitude: (.*)/', $results[$i], $lon);

Any ideas?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

you get necessary set a time out just for file_get_contents, but you can for

Code: Select all

fopen();
with

Code: Select all

stream_set_timeout();
but you could change.. but it changes the timeout for everything...

Code: Select all

ini_set('default_socket_timeout', $seconds);

and btw i found this out with 20 secs of googling
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

Ya I found the same stuff... but I dont know how to use it with my code.
Post Reply