transparencia Posted January 18, 2009 Share Posted January 18, 2009 I got this error on a code that used to work fine. Warning: file_get_contents(http://api.hostip.info/country.php?ip=myip) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in /home/account/public_html/index.php on line 63 Line 63 of index.php says: $ip = ($_SERVER["REMOTE_ADDR"]); What is wrong? Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/ Share on other sites More sharing options...
premiso Posted January 18, 2009 Share Posted January 18, 2009 Post about 10-20 lines of code around line 63. Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740080 Share on other sites More sharing options...
transparencia Posted January 18, 2009 Author Share Posted January 18, 2009 Here it is: $ip = ($_SERVER["REMOTE_ADDR"]); $country = file_get_contents("http://api.hostip.info/country.php?ip=".$ip); switch ($country) { case 'US': display something break; case 'UK': display other thing break; etc.... Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740084 Share on other sites More sharing options...
Kieran Menor Posted January 18, 2009 Share Posted January 18, 2009 My guess would be that the cause is the target (http://api.hostip.info/country.php?ip=myip) actually returning a HTTP 500 error, as described in the error message. Just a hunch. :3 Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740085 Share on other sites More sharing options...
.josh Posted January 18, 2009 Share Posted January 18, 2009 The error tells you what's wrong. you make a call to file_get_contents to a url and it failed, because there was a 500 error at that url. Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740086 Share on other sites More sharing options...
transparencia Posted January 18, 2009 Author Share Posted January 18, 2009 Indeed, my bad. I presumed the error was on my server. Is there a way to avoid this, some code to check if everything is alright with their server? Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740087 Share on other sites More sharing options...
Kieran Menor Posted January 18, 2009 Share Posted January 18, 2009 It's just a warning, so you could stuff an @ in front of the file_get_contents. It would still fail, but just shut up about it. Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740090 Share on other sites More sharing options...
xtopolis Posted January 18, 2009 Share Posted January 18, 2009 @transparencia You could cURL the url and check the headers for validity. @Boom.dk That's a fairly serious warning.. Better to fix it than avoid it. Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740091 Share on other sites More sharing options...
.josh Posted January 18, 2009 Share Posted January 18, 2009 you could always suppress it and check what $country is: $country = @file_get_contents("http://api.hostip.info/country.php?ip=".$ip); if ($country) { // success, do something } else { // fail, do something } Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740092 Share on other sites More sharing options...
.josh Posted January 18, 2009 Share Posted January 18, 2009 @Boom.dk That's a fairly serious warning.. Better to fix it than avoid it. You can't fix that sort of thing. He has no control over some 3rd party site. The 'better' thing to do would be to suppress it and check if it failed or not (as I showed in previous post) Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740095 Share on other sites More sharing options...
Kieran Menor Posted January 18, 2009 Share Posted January 18, 2009 What makes it particularly serious? It's just a HTTP error. In any case, the target script seems to be broken, so I don't think there's much to do about it. Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740096 Share on other sites More sharing options...
.josh Posted January 18, 2009 Share Posted January 18, 2009 What makes it particularly serious? It's just a HTTP error. In any case, the target script seems to be broken, so I don't think there's much to do about it. well it *could* be serious if the rest of your site depends on info gathered from the file_get_contents... Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740098 Share on other sites More sharing options...
xtopolis Posted January 18, 2009 Share Posted January 18, 2009 I guess I misinterpreted what was meant when using "suppress". As CV showed, you could suppress and then handle. I assumed Boom.dk meant suppress and forget as he did not post any other solution. And yes, I also imagined that a country check might be used for locale settings on the page or something. But it was mainly the "supress and forget" assumption. Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740101 Share on other sites More sharing options...
Kieran Menor Posted January 18, 2009 Share Posted January 18, 2009 Well, yeah. @transparencia: From what I can gather, you are trying to determine a user's location from his/her IP. I think the best approach to solve this problem is to have a local list of IP ranges and their matching countries rather than relying on a 3rd party. The IP ranges can be obtained from a site like this one: http://www.ipaddresslocation.org/ Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740102 Share on other sites More sharing options...
transparencia Posted January 18, 2009 Author Share Posted January 18, 2009 That is want I'm going to do. It's better that way, thank you all for your quick help. Quote Link to comment https://forums.phpfreaks.com/topic/141386-solved-php-error/#findComment-740110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.