IP Geolocation API php SDK
In this document, you will go through the basic steps to use IP Geolocation API php SDK.
You need a valid 'ipgeolocation API key' to use this SDK. Sign up here and get your free API key if you don’t have one.
System Requirements
Internet connection is required to run this component.
Basic Usage
Call method get_geolocation($apiKey, $ip, $lang, $fields, $excludes) passing API key and 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_geolocation method as described below:
- $lang
Pass the language parameter to get the geolocation information in a language other than English. By default, it is set to English language. IPGeolocation provides response in the following languages:
- English (en)
- German (de)
- Russian (ru)
- Japanese (ja)
- French (fr)
- Chinese Simplified (cn)
- Spanish (es)
- Czech (cs)
- Italian (it)
- Persian (fa)
- Korean (ko)
- $fields
Pass the fields parameter to get the specified fields only. By default, it is set to get all the fields in the response
- $excludes
Pass the exlcludes parameter to get remove the unnecessary fields from the response. By default, it set to not to exclude any fields.
php
?php
$apiKey = "PUT_YOUR_API_KEY_HERE";
$ip = "CLIENT_IP_ADDRESS";
$location = get_geolocation($apiKey, $ip);
$decodedLocation = json_decode($location, true);
echo "<pre>;";
print_r($decodedLocation);
echo "</pre>;";
function get_geolocation($apiKey, $ip, $lang = "en", $fields = "*", $excludes = "") {
$url = "https://api.ipgeolocation.io/ipgeo?apiKey=".$apiKey."&ip=".$ip."&lang=".$lang."&fields=".$fields."&excludes=".$excludes;
$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',
'User-Agent: '.$_SERVER['HTTP_USER_AGENT']
));
return curl_exec($cURL);
}
?>
Example
Here is an example to get the geolocation for a list of IP addresses and display the result as a table:
php
<style>;
table, th, tr, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px 30px;
}
</style>;
<?php
$apiKey = "PUT_YOUR_API_KEY_HERE";
$ips = array("3.3.3.3", "4.4.4.4", "5.5.5.5", "6.6.6.6", "7.7.7.7");
echo "<table>";
echo "<tr>";
echo "<th>IP</th>";
echo "<th>Continent</th>";
echo "<th>Country</th>";
echo "<th>Organization</th>";
echo "<th>ISP</th>";
echo "<th>Languages</th>";
echo "<th>Is EU Member?</th>";
echo "<th>Currency</th>";
echo "<th>Timezone</th>";
echo "</tr>";
foreach ($ips as $ip) {
$location = get_geolocation($apiKey, $ip);
$decodedLocation = json_decode($location, true);
echo "<tr>";
if ($decodedLocation['message'] != '') {
echo "<td>;".$ip."</td>;";
echo "<td>;".$decodedLocation['message']."</td>;";
} else {
echo "<td>;".$decodedLocation['ip']."</td>;";
echo "<td>;".$decodedLocation['continent_name']." (".$decodedLocation['continent_code'].")</td>;";
echo "<td>;".$decodedLocation['country_name']." (".$decodedLocation['country_code2'].")</td>;";
echo "<td>;".$decodedLocation['organization']."</td>;";
echo "<td>;".$decodedLocation['isp']."</td>;";
echo "<td>;".$decodedLocation['languages']."</td>;";
if ($decodedLocation['is_eu'] == true) {
echo "<td>;Yes</td>;";
} else {
echo "<td>;No</td>;";
}
echo "<td>;".$decodedLocation['currency']['name']."</td>;";
echo "<td>;".$decodedLocation['time_zone']['name']."</td>;";
}
echo "</tr>;";
}
echo "</table>;";
function get_geolocation($apiKey, $ip, $lang = "en", $fields = "*", $excludes = "") {
$url = "https://api.ipgeolocation.io/ipgeo?apiKey=".$apiKey."&ip=".$ip."&lang=".$lang."&fields=".$fields."&excludes=".$excludes;
$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',
'User-Agent: '.$_SERVER['HTTP_USER_AGENT']
));
return curl_exec($cURL);
}
?>;
Source Code
The complete source code for this SDK is available at Github.