Getting country name from IP php with iptolatlng.com

What i am trying to do is get the country name from the IP address. I have created a class named General and in that class I have a function called GetIPCountry($ip) which returns the jason decoded array from the iptolatlng.com

public function GetIPCountry($ip){
    $details = json_decode(file_get_contents("http://www.iptolatlng.com?`ip=".urlencode($ip)));`
    return $details->countryFullName;
}

when i send my ip like this in my localhost:

$country = $general->GetIPCountry('***.***.***.**');

it works just fine!

But when I send upload the same file and run it in my server, there is no output.

I thought since I am on the server I should try the $_SERVER['REMOTE_ADDR']; too but this doesn't show any result too. I suspect this could be a server error too, I am hosted at iPage Please help me, Thanks in advance!

Answers


Just a quick question: what does $ip contain when you host it on the remote server? In essence, if you were to print out $ip, does it contain your IP address? This will help you to figure out if it is an issue with getting the IP address of the connected computer or otherwise.


Are you sending 127.0.0.1 as the IP? If you are asking the service the location of IP 127.0.0.1, it won't be able to tell you what your location is. I had this problem when running your code from my localhost.


Here there are two plugins that work

http://taylorlopes.com/?p=636

and this one: http://www.geoplugin.com/

i preferred the second one


Not directly answering your question but might help - why not use the geoip PECL extension? http://www.php.net/manual/en/function.geoip-country-code-by-name.php

With this you won't have to go out to an external web server for the result, and won't be dependent on a 3rd party service downtime.


Need Your Help

C++ - Replace spaces with %20

c++ visual-c++

I am looking for a way to prepare a string for use as a URL.

Find difference from sum of the odd columns and sum of the even rows in matrix in C

c matrix sum difference

I need to find the difference from the sum of odd columns and sum of the even rows in matrix.