Skip to content

Commit 790a895

Browse files
authored
Update README.md
1 parent f68b864 commit 790a895

1 file changed

Lines changed: 20 additions & 28 deletions

File tree

README.md

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,38 @@
11
# ip-geolocation-api-php
22

33
## 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
84

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.
116

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.
138

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.
1510

16-
And replace 103.255.5.105 (ip) with your client ip.
17-
1811
```
1912
<?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";
2115
16+
$response = get_location($apikey, $clientIp);
2217
$resArr = array();
2318
$resArr = json_decode($response);
24-
2519
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);
2632
27-
function get_web_page($url) {
33+
curl_close($cURL);
2834
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;
4436
}
4537
?>
4638
```

0 commit comments

Comments
 (0)