Horje
Learn PHP Network Tutorial Code

Tips (Total 1)


# Tips-1) How to get country details from ip address using PHP

Use API to get the details of the visitor’s IP: Here, we are going to use the geoPlugin API to get the details of the visitor. The api will deliver a json object, which can convert to a PHP variable.

How to extract ip to country?

Just use following way echo
index.php
Example: PHP

<?php 
// PHP code to obtain country, city,  
// continent, etc using IP Address 
  
$ip = '52.25.109.230'; 
  
// Use JSON encoded string and converts 
// it into a PHP variable 
$ipdat = @json_decode(file_get_contents( 
    "http://www.geoplugin.net/json.gp?ip=" . $ip)); 
   
echo 'Country Name: ' . $ipdat->geoplugin_countryName . "\n"; 
echo 'City Name: ' . $ipdat->geoplugin_city . "\n"; 
echo 'Continent Name: ' . $ipdat->geoplugin_continentName . "\n"; 
echo 'Latitude: ' . $ipdat->geoplugin_latitude . "\n"; 
echo 'Longitude: ' . $ipdat->geoplugin_longitude . "\n"; 
echo 'Currency Symbol: ' . $ipdat->geoplugin_currencySymbol . "\n"; 
echo 'Currency Code: ' . $ipdat->geoplugin_currencyCode . "\n"; 
echo 'Timezone: ' . $ipdat->geoplugin_timezone; 
   
?>

Output should be:

How to extract ip to country?

Share on: