0

Hello i use this api: http://ipapi.co/json to get country, city and real ip address in localhost (127.0.0.1) work perfect, but when i put my code to my website my code not show address of the client, but address of the server how can i fix this ?

I can get client ip address with $_SERVER['REMOTE_ADDR']; but i need this api to get country and city with: http://ipapi.co/json.

Function TTT() {
    $ip = false;
    $city = false;
    $region = false;
    $country = false;
    $country_code = false;

    if($json = @file_get_contents("http://www.geoplugin.net/json.gp")) {
      $obj = json_decode($json);
      if(isset($obj->geoplugin_request) && $obj->geoplugin_request != false) {
        $ip = "IP: ". $obj->geoplugin_request. "</br>";
      }
      if(isset($obj->geoplugin_city) && $obj->geoplugin_city != false) {
        $city = "City: ". $obj->geoplugin_city. "</br>";
      }
      if(isset($obj->geoplugin_city) && $obj->geoplugin_city != false) {
        $region = "Region: ". $obj->geoplugin_city. "</br>";
      }
      if(isset($obj->geoplugin_countryName) && $obj->geoplugin_countryName != false) {
        $country = "Country: ". $obj->geoplugin_countryName. "</br>";
      }
      if(isset($obj->geoplugin_countryCode) && $obj->geoplugin_countryCode != false) {
        $country_code = "Country Code: ". $obj->geoplugin_countryCode. "</br>";
      }

      return $ip. $city. $region. $country. $country_code;
    }
}

echo TTT();
LOVE PHP
  • 53
  • 5
  • me gives my data ?, – Álvaro Touzón Mar 21 '17 at 20:11
  • in your example you use `http://www.geoplugin.net/json.gp` not `http://ipapi.co/json` – Constantin Galbenu Mar 21 '17 at 20:11
  • file_get_contents make a request from server, not client. you CANNOT get client's geo_code without using $_SERVER['REMOTE_ADDR'] or using javascript which visits ipapi website from client's browser and submits it with ajax. Do keep in mind that relying on javascript is NOT secure because client can just edit javascript. What you want to do is record `$_SERVER['REMOTE_ADDR']` then do geo_ip lookup on it. You can either download entire IP to GEO mapping and do it yourself, or use free services that do it for you https://freegeoip.net/ – Dimi Mar 21 '17 at 20:14
  • From the remote service's point of view all the requests come from the same IP, the IP of your server. – Constantin Galbenu Mar 21 '17 at 20:14
  • You won't be able to do that with an API that is meant to locate you using the address from which the file is requested. You would need an IP geocoder that allows to pass arbitrary addresses. Just a quick google search gave me an API to do this (http://ip-api.com/) – ffflabs Mar 21 '17 at 20:17
  • @LOVEPHP Do NOT do it in javascript way. It is marginally better than not doing any geo lookup at all. If you absolutely have to then either use Ajax or something like this http://stackoverflow.com/questions/10642289/return-html-content-as-a-string-given-url-javascript-function ... But ipapi.co already supports remote IP addresses. so just use that. – Dimi Mar 21 '17 at 20:18
  • `http://ipapi.co/IP_TO_CHECK/json` or check this, [http://www.geoplugin.com/webservices/php](http://www.geoplugin.com/webservices/php) – ajai Jothi Mar 21 '17 at 20:20

2 Answers2

3

Using the api at http://ipapi.co/json

$obj = json_decode($json);

Would put this JSON in an associative array:

{
    "ip": "00.00.00.00",
    "city": "Atlanta",
    "region": "Georgia",
    "country": "US",
    "postal": "31532",
    "latitude": 34.461,
    "longitude": -85.9877,
    "timezone": "America/New_York"
}

Access City and Region Like this:

if(isset($obj->city) && $obj->city != false) {
    $city = "City: ". $obj->city. "</br>";
  }
if(isset($obj->region) && $obj->region != false) {
    $region = "Region: ". $obj->region. "</br>";
  }

It appears that you're not using the API you originally stated you were using.

2

You must include the IP of your visitor in the request to the remote service. From what I've tested, you can put the client IP address in the URL like this:

function TTT() {
    $ip = false;
    $city = false;
    $region = false;
    $country = false;
    $country_code = false;

    if($json = @file_get_contents("http://ipapi.co/" . $_SERVER['REMOTE_ADDR'] . "/json")) {
      $obj = json_decode($json);
      if(isset($obj->ip) && $obj->ip != false) {
        $ip = "IP: ". $obj->ip. "</br>";
      }
      if(isset($obj->city) && $obj->city != false) {
        $city = "City: ". $obj->city. "</br>";
      }
      if(isset($obj->region) && $obj->region != false) {
        $region = "Region: ". $obj->region. "</br>";
      }
      if(isset($obj->country) && $obj->country != false) {
        $country = "Country: ". $obj->country. "</br>";
      }

      return $ip. $city. $region. $country;
    }
}

echo TTT();

Note: the response from that API is like this:

{
  "ip": "8.8.8.8",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "postal": "94035",
  "latitude": 37.386,
  "longitude": -122.0838,
  "timezone": "America/Los_Angeles"
}
Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54