Skip to content

Commit b8d5aa4

Browse files
Script bug removed, Example added
1 parent 7256041 commit b8d5aa4

1 file changed

Lines changed: 82 additions & 11 deletions

File tree

README.md

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# IP Geolocation API PHP SDK
22

33
## Introduction
4+
45
IPGeolocation API is the solution to identify country code (ISO2 and ISO3 standard), country name, continent code, continent name, country capital, state/province, district, city, zip code, latitude and longitude of city, is country belongs to Europian Union, calling code, top level domain (TLD), languages, country flag, internet service provider (ISP), connection type, organization, geoname ID, currency code, currency name, time zone ID, time zone offset, current time in the time zone, is time zone in daylight saving time, and total daylight savings. This document provides important information to help you get up to speed with IPGeolocation API using IP Geolocation API PHP SDK.
56

67
Developers can use this PHP SDK for software and web projects related to, but not limited to:
@@ -18,35 +19,105 @@ In this document, you will go through the basic steps to use IP Geolocation API
1819
You need a valid 'IPGeolocation API key' to use this SDK. [Sign up](https://ipgeolocation.io/signup) here and get your free API key if you don't have one.
1920

2021
## System Requirements
22+
2123
Internet connection is required to run this component.
2224

2325
## Basic Usage
26+
2427
Call method **get_location($apikey, $clientIp)** passing *IP address (optional)* and *API key* as parameters and it will return IP Geolocation API response.
2528
Note: If you want IP to geolocation for the machine calling IPGeolocation API, leave out the *IP address* parameter.
2629

2730
```php
2831
<?php
29-
$clientIp = "IP_ADDRESS";
30-
$apikey = "YOUR_API_KEY";
32+
$clientIp = null;
33+
$apikey = "PUT_YOUR_API_KEY_HERE";
3134
$response = get_location($apikey, $clientIp);
32-
$resArr = array();
33-
$resArr = json_decode($response);
34-
echo "<pre>"; print_r($resArr); echo "</pre>";
35+
$json = array();
36+
$json = json_decode($response, true);
3537

38+
echo "<pre>";
39+
print_r($json);
40+
echo "</pre>";
41+
3642
function get_location($apiKey, $ip = null) {
3743
$url = "https://api.ipgeolocation.io/ipgeo?apiKey=".$apiKey."&ip=".$ip;
3844
$cURL = curl_init();
39-
45+
4046
curl_setopt($cURL, CURLOPT_URL, $url);
4147
curl_setopt($cURL, CURLOPT_HTTPGET, true);
48+
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
4249
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
43-
'Content-Type: application/json',
44-
'Accept: application/json'
50+
'Content-Type: application/json',
51+
'Accept: application/json'
4552
));
53+
return curl_exec($cURL);
54+
}
55+
?>
56+
```
57+
58+
### Example
59+
60+
Here is an example to get the geolocation for a list of IP addresses and display the result as a table:
61+
62+
```php
63+
<style>
64+
table, th, tr, td {
65+
border: 1px solid black;
66+
border-collapse: collapse;
67+
}
68+
69+
th, td {
70+
padding: 5px 30px;
71+
}
72+
</style>
4673

47-
$content = curl_exec($cURL);
48-
curl_close($cURL);
49-
return $content;
74+
<?php
75+
$ips = array("3.3.3.3", "4.4.4.4", "5.5.5.5", "6.6.6.6", "7.7.7.7");
76+
$apikey = "PUT_YOUR_API_KEY_HERE";
77+
78+
echo "<table>";
79+
echo "<tr>";
80+
echo "<th>IP</th>";
81+
echo "<th>Continent</th>";
82+
echo "<th>Country</th>";
83+
echo "<th>Organization</th>";
84+
echo "<th>ISP</th>";
85+
echo "<th>Languages</th>";
86+
echo "<th>Is EU?</th>";
87+
echo "</tr>";
88+
89+
foreach($ips as $ip) {
90+
$location = get_location($apikey, $ip);
91+
$decodedLocation = json_decode($location);
92+
93+
echo "<tr>";
94+
echo "<td>$decodedLocation->ip</td>";
95+
echo "<td>$decodedLocation->continent_name ($decodedLocation->continent_code)</td>";
96+
echo "<td>$decodedLocation->country_name ($decodedLocation->country_code2)</td>";
97+
echo "<td>$decodedLocation->organization</td>";
98+
echo "<td>$decodedLocation->isp</td>";
99+
echo "<td>$decodedLocation->languages</td>";
100+
if($decodedLocation->is_eu == true) {
101+
echo "<td>Yes</td>";
102+
} else {
103+
echo "<td>No</td>";
104+
}
105+
echo "</tr>";
106+
}
107+
echo "</table>";
108+
109+
function get_location($apiKey, $ip = null) {
110+
$url = "https://api.ipgeolocation.io/ipgeo?apiKey=".$apiKey."&ip=".$ip;
111+
$cURL = curl_init();
112+
113+
curl_setopt($cURL, CURLOPT_URL, $url);
114+
curl_setopt($cURL, CURLOPT_HTTPGET, true);
115+
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
116+
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
117+
'Content-Type: application/json',
118+
'Accept: application/json'
119+
));
120+
return curl_exec($cURL);
50121
}
51122
?>
52123
```

0 commit comments

Comments
 (0)