Use the ipwhois.io IP Geolocation API to quickly and simply integrate IP geolocation into your script or website. Save yourself the hassle of setting up local GeoIP libraries and having to remember to regularly update the data. You can use ipwhois.io to filter out bot traffic, customize content based on visitor's location, display full country names, perform bulk IP geolocation, and more.
You can call the API by sending HTTP GET requests to http://free.ipwhois.io/json/{IP}
{IP}
can be an IPv4 or IPv6 address, or none to use the current IP address.
Note: Complete documentation to use this SDK is also available at IP Geolocation API PHP SDK - Documentation.
Internet connection is required to run this component.
Call method get_ipwhois($ip, $format, $lang)
passing IP address as parameters (rest of the parameters are optional) and it will return the Geolocation for the passed IP address. To customize the geolocation response, you can pass the other parameters to get_ipwhois()
method as described below:
-
$format
Based on your choice, the ipwhois API can deliver results in:- json (default)
- xml
- line
-
$lang
Pass the language parameter to get the geolocation information in a language other than English. By default, it is set to English language.
ipwhois provides response in the following languages:- en - English (default)
- de - Deutsch (German)
- es - Español (Spanish)
- pt-BR - Español - Argentina (Spanish)
- fr - Français (French)
- ja - 日本語 (Japanese)
- zh-CN - ** (Chinese)
- ru - Русский (Russian)
<?php
$ip = "CLIENT_IP_ADDRESS";
$location = get_ipwhois($ip);
$decodedLocation = json_decode($location, true);
echo "<pre>";
print_r($decodedLocation);
echo "</pre>";
function get_ipwhois($ip, $format = "json", $lang = "en") {
$url = "http://free.ipwhois.io/".$format."/".$ip."?lang=".$lang;
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
));
return curl_exec($cURL);
}
?>