|
1 | | -# ip-geolocation-api-php |
| 1 | +# IP Geolocation API PHP SDK |
| 2 | +You need a valid IPGeolocation API key to use this SDK. Sign up(https://ipgeolocation.io/signup?plan=1) if you don’t have API key. |
2 | 3 |
|
3 | 4 | ## Quick Start Guide |
4 | | - |
5 | | - There is a methode get_location($apikey, $clientIp) in below given snippet which takes apiKey and clientIp as parameter and returns the response from ipgeolocation. |
6 | | - |
7 | | - All you have to do is replace keyword "YOUR_CLIENT_IP" with your valid client ip and "YOUR_API_KEY" with valid apiKey provided by ipgeolocation. |
8 | | - |
9 | | - Note: This snippet requires a valid IPGeolocation API key. Sign up (https://ipgeolocation.io/signup?plan=1) if you don’t have API key. And you may leave YOUR_CLIENT_IP empty. |
10 | | - |
11 | | -``` |
12 | | -<?php |
13 | | -$clientIp = "YOUR_CLIENT_IP"; |
14 | | -$apikey = "YOUR_API_KEY"; |
15 | | -
|
16 | | -$response = get_location($apikey, $clientIp); |
17 | | -$resArr = array(); |
18 | | -$resArr = json_decode($response); |
19 | | -echo "<pre>"; print_r($resArr); echo "</pre>"; |
20 | | -function get_location($apiKey, $ip = null) { |
21 | | - $url = "https://api.ipgeolocation.io/ipgeo?apiKey=".$apiKey."&ip=".$ip; |
22 | | - $cURL = curl_init(); |
23 | | - curl_setopt($cURL, CURLOPT_URL, $url); |
24 | | - curl_setopt($cURL, CURLOPT_HTTPGET, true); |
| 5 | +Call method **get_location($apikey, $clientIp)** passing *API key* and *IP address (optional)* as parameters and it will return IPGeolocation API response. |
| 6 | +Note: If you want IP to geolocation for machine calling IPGeolocation API, don't pass the *IP address* parameter. |
25 | 7 |
|
26 | | -curl_setopt($cURL, CURLOPT_HTTPHEADER, array( |
27 | | - 'Content-Type: application/json', |
28 | | - 'Accept: application/json' |
29 | | -)); |
30 | | -
|
31 | | - $content = curl_exec($cURL); |
| 8 | +### Basic Usage |
| 9 | +```php |
| 10 | +<?php |
| 11 | + $clientIp = "IP_ADDRESS"; |
| 12 | + $apikey = "YOUR_API_KEY"; |
| 13 | + |
| 14 | + $response = get_location($apikey, $clientIp); |
| 15 | + $resArr = array(); |
| 16 | + $resArr = json_decode($response); |
| 17 | + |
| 18 | + echo "<pre>"; print_r($resArr); echo "</pre>"; |
| 19 | + function get_location($apiKey, $ip = null) { |
| 20 | + $url = "https://api.ipgeolocation.io/ipgeo?apiKey=".$apiKey."&ip=".$ip; |
| 21 | + $cURL = curl_init(); |
| 22 | + curl_setopt($cURL, CURLOPT_URL, $url); |
| 23 | + curl_setopt($cURL, CURLOPT_HTTPGET, true); |
32 | 24 |
|
33 | | - curl_close($cURL); |
| 25 | + curl_setopt($cURL, CURLOPT_HTTPHEADER, array( |
| 26 | + 'Content-Type: application/json', |
| 27 | + 'Accept: application/json' |
| 28 | + )); |
34 | 29 |
|
35 | | - return $content; |
36 | | -} |
| 30 | + $content = curl_exec($cURL); |
| 31 | + curl_close($cURL); |
| 32 | + return $content; |
| 33 | + } |
37 | 34 | ?> |
38 | 35 | ``` |
0 commit comments