|
1 | 1 | # ip-geolocation-api-php |
2 | 2 |
|
3 | 3 | ## Quick Start Guide |
4 | | - |
5 | | - Following is a php code snippet that gets the golocation data from the ip addess given. |
6 | | - |
7 | | - There is a methode get_web_page which takes url as parameter and returns the response from ipgeolocation. All you have to do is call the get_web_page function with the correct url |
8 | 4 |
|
9 | | - What constitutes a correct URL? |
10 | | - the correct url is created by |
| 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. |
11 | 6 |
|
12 | | - Url takes one required parameter apiKey. |
| 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. |
13 | 8 |
|
14 | | - Note: This Url requires a valid IPGeolocation API key. Sign up (https://ipgeolocation.io/signup?plan=1) if you don’t have one. |
| 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. |
15 | 10 |
|
16 | | - And replace 103.255.5.105 (ip) with your client ip. |
17 | | - |
18 | 11 | ``` |
19 | 12 | <?php |
20 | | -$response = get_web_page("https://api.ipgeolocation.io/ipgeo?apiKey=YOUR_API_KEY&ip=YOUR_CLIENTS_IP"); |
| 13 | +$clientIp = "YOUR_CLIENT_IP"; |
| 14 | +$apikey = "YOUR_API_KEY"; |
21 | 15 |
|
| 16 | +$response = get_location($apikey, $clientIp); |
22 | 17 | $resArr = array(); |
23 | 18 | $resArr = json_decode($response); |
24 | | -
|
25 | 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); |
| 25 | +
|
| 26 | +curl_setopt($cURL, CURLOPT_HTTPHEADER, array( |
| 27 | + 'Content-Type: application/json', |
| 28 | + 'Accept: application/json' |
| 29 | +)); |
| 30 | +
|
| 31 | + $content = curl_exec($cURL); |
26 | 32 |
|
27 | | -function get_web_page($url) { |
| 33 | + curl_close($cURL); |
28 | 34 |
|
29 | | - $cURL = curl_init(); |
30 | | - |
31 | | - curl_setopt($cURL, CURLOPT_URL, $url); |
32 | | - curl_setopt($cURL, CURLOPT_HTTPGET, true); |
33 | | - |
34 | | - curl_setopt($cURL, CURLOPT_HTTPHEADER, array( |
35 | | - 'Content-Type: application/json', |
36 | | - 'Accept: application/json' |
37 | | - )); |
38 | | - |
39 | | - $content = curl_exec($cURL); |
40 | | - |
41 | | - curl_close($cURL); |
42 | | - |
43 | | - return $content; |
| 35 | + return $content; |
44 | 36 | } |
45 | 37 | ?> |
46 | 38 | ``` |
0 commit comments