Reputation: 53
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();
Upvotes: 0
Views: 1984
Reputation: 197
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.
Upvotes: 3
Reputation: 17683
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"
}
Upvotes: 2