Skip to content

Commit cc9a5cb

Browse files
IP2Location API
0 parents  commit cc9a5cb

4 files changed

Lines changed: 267 additions & 0 deletions

File tree

LICENSE.TXT

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 IP2Location.com
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following aconditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# IP2Location PHP API
2+
3+
This module allows user to geolocate the country, region, city, coordinates, zip code, ISP, domain name, timezone, connection speed, IDD code, area code, weather station code, weather station name, MCC, MNC, mobile brand name, elevation and usage type that any IP address or host name originates from by calling the **IP2Location API**.
4+
5+
6+
## Getting Started
7+
To begin, an API key is required for this module to function. Find further information at https://www.ip2location.com/web-service
8+
9+
10+
11+
## Usage
12+
13+
```
14+
<?php
15+
16+
class IP2LocationAPI
17+
{
18+
public $countryCode;
19+
public $countryName;
20+
public $regionName;
21+
public $cityName;
22+
public $latitude;
23+
public $longitude;
24+
public $zipCode;
25+
public $isp;
26+
public $domain;
27+
public $timeZone;
28+
public $netSpeed;
29+
public $iddCode;
30+
public $areaCode;
31+
public $weatherStationCode;
32+
public $weatherStationName;
33+
public $mcc;
34+
public $mnc;
35+
public $mobileBrand;
36+
public $elevation;
37+
public $usageType;
38+
39+
protected $apiKey;
40+
protected $package;
41+
protected $useSSL;
42+
43+
public function __construct($apiKey = '', $package = 'WS24', $useSSL = false)
44+
{
45+
$this->apiKey = $apiKey;
46+
$this->package = $package;
47+
$this->useSSL = $useSSL;
48+
}
49+
50+
public function query($ip)
51+
{
52+
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
53+
return false;
54+
}
55+
56+
$response = $this->get('http' . (($this->useSSL) ? 's' : '') . '://api.ip2location.com/?' . http_build_query([
57+
'key' => $this->apiKey,
58+
'ip' => $ip,
59+
'package' => $this->package,
60+
'format' => 'json',
61+
]));
62+
63+
if (($json = json_decode($response)) === null) {
64+
return false;
65+
}
66+
67+
if (isset($json->response)) {
68+
return false;
69+
}
70+
71+
$this->countryCode = (string) (isset($json->country_code)) ? $json->country_code :'N/A';
72+
$this->countryName = (string) (isset($json->country_name)) ? $json->country_name :'N/A';
73+
$this->regionName = (string) (isset($json->region_name)) ? $json->region_name :'N/A';
74+
$this->cityName = (string) (isset($json->city_name)) ? $json->city_name :'N/A';
75+
$this->latitude = (float) (isset($json->latitude)) ? $json->latitude :'N/A';
76+
$this->longitude = (float) (isset($json->longitude)) ? $json->longitude :'N/A';
77+
$this->zipCode = (string) (isset($json->zip_code)) ? $json->zip_code :'N/A';
78+
$this->timeZone = (string) (isset($json->time_zone)) ? $json->time_zone :'N/A';
79+
$this->isp = (string) (isset($json->isp)) ? $json->isp :'N/A';
80+
$this->domain = (string) (isset($json->domain)) ? $json->domain :'N/A';
81+
$this->netSpeed = (string) (isset($json->net_speed)) ? $json->net_speed :'N/A';
82+
$this->iddCode = (string) (isset($json->idd_code)) ? $json->idd_code :'N/A';
83+
$this->areaCode = (string) (isset($json->area_code)) ? $json->area_code :'N/A';
84+
$this->weatherStationCode = (string) (isset($json->weather_station_code)) ? $json->weather_station_code :'N/A';
85+
$this->weatherStationName = (string) (isset($json->weather_station_name)) ? $json->weather_station_name :'N/A';
86+
$this->mcc = (string) (isset($json->mcc)) ? $json->mcc :'N/A';
87+
$this->mnc = (string) (isset($json->mnc)) ? $json->mnc :'N/A';
88+
$this->mobileBrand = (string) (isset($json->mobile_brand)) ? $json->mobile_brand :'N/A';
89+
$this->elevation = (int) (isset($json->elevation)) ? $json->elevation :'N/A';
90+
$this->usageType = (string) (isset($json->usage_type)) ? $json->usage_type :'N/A';
91+
92+
return true;
93+
}
94+
95+
private function get($url)
96+
{
97+
$ch = curl_init();
98+
99+
curl_setopt($ch, CURLOPT_URL, $url);
100+
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
101+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
102+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
103+
curl_setopt($ch, CURLOPT_USERAGENT, 'IP2LocationAPI_PHP-1.0.0');
104+
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
105+
$response = curl_exec($ch);
106+
107+
return $response;
108+
}
109+
}
110+
111+
?>
112+
```
113+
114+

class.IP2LocationAPI.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
class IP2LocationAPI
4+
{
5+
public $countryCode;
6+
public $countryName;
7+
public $regionName;
8+
public $cityName;
9+
public $latitude;
10+
public $longitude;
11+
public $zipCode;
12+
public $isp;
13+
public $domain;
14+
public $timeZone;
15+
public $netSpeed;
16+
public $iddCode;
17+
public $areaCode;
18+
public $weatherStationCode;
19+
public $weatherStationName;
20+
public $mcc;
21+
public $mnc;
22+
public $mobileBrand;
23+
public $elevation;
24+
public $usageType;
25+
26+
protected $apiKey;
27+
protected $package;
28+
protected $useSSL;
29+
30+
public function __construct($apiKey = '', $package = 'WS24', $useSSL = false)
31+
{
32+
$this->apiKey = $apiKey;
33+
$this->package = $package;
34+
$this->useSSL = $useSSL;
35+
}
36+
37+
public function query($ip)
38+
{
39+
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
40+
return false;
41+
}
42+
43+
$response = $this->get('http' . (($this->useSSL) ? 's' : '') . '://api.ip2location.com/?' . http_build_query([
44+
'key' => $this->apiKey,
45+
'ip' => $ip,
46+
'package' => $this->package,
47+
'format' => 'json',
48+
]));
49+
50+
if (($json = json_decode($response)) === null) {
51+
return false;
52+
}
53+
54+
if (isset($json->response)) {
55+
return false;
56+
}
57+
58+
$this->countryCode = (string) (isset($json->country_code)) ? $json->country_code :'N/A';
59+
$this->countryName = (string) (isset($json->country_name)) ? $json->country_name :'N/A';
60+
$this->regionName = (string) (isset($json->region_name)) ? $json->region_name :'N/A';
61+
$this->cityName = (string) (isset($json->city_name)) ? $json->city_name :'N/A';
62+
$this->latitude = (float) (isset($json->latitude)) ? $json->latitude :'N/A';
63+
$this->longitude = (float) (isset($json->longitude)) ? $json->longitude :'N/A';
64+
$this->zipCode = (string) (isset($json->zip_code)) ? $json->zip_code :'N/A';
65+
$this->timeZone = (string) (isset($json->time_zone)) ? $json->time_zone :'N/A';
66+
$this->isp = (string) (isset($json->isp)) ? $json->isp :'N/A';
67+
$this->domain = (string) (isset($json->domain)) ? $json->domain :'N/A';
68+
$this->netSpeed = (string) (isset($json->net_speed)) ? $json->net_speed :'N/A';
69+
$this->iddCode = (string) (isset($json->idd_code)) ? $json->idd_code :'N/A';
70+
$this->areaCode = (string) (isset($json->area_code)) ? $json->area_code :'N/A';
71+
$this->weatherStationCode = (string) (isset($json->weather_station_code)) ? $json->weather_station_code :'N/A';
72+
$this->weatherStationName = (string) (isset($json->weather_station_name)) ? $json->weather_station_name :'N/A';
73+
$this->mcc = (string) (isset($json->mcc)) ? $json->mcc :'N/A';
74+
$this->mnc = (string) (isset($json->mnc)) ? $json->mnc :'N/A';
75+
$this->mobileBrand = (string) (isset($json->mobile_brand)) ? $json->mobile_brand :'N/A';
76+
$this->elevation = (int) (isset($json->elevation)) ? $json->elevation :'N/A';
77+
$this->usageType = (string) (isset($json->usage_type)) ? $json->usage_type :'N/A';
78+
79+
return true;
80+
}
81+
82+
private function get($url)
83+
{
84+
$ch = curl_init();
85+
86+
curl_setopt($ch, CURLOPT_URL, $url);
87+
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
88+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
89+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
90+
curl_setopt($ch, CURLOPT_USERAGENT, 'IP2LocationAPI_PHP-1.0.0');
91+
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
92+
$response = curl_exec($ch);
93+
94+
return $response;
95+
}
96+
}

example.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
require_once 'class.IP2LocationAPI.php';
4+
5+
$apiKey = 'YOUR_API_KEY';
6+
$package = 'WS24'; // Package: WS1 - WS24
7+
$useSSL = false; // Use HTTP or HTTPS (Secure, but slower)
8+
9+
$ip = '8.8.8.8';
10+
11+
$location = new IP2LocationAPI($apiKey, $package, $useSSL);
12+
13+
if (!$location->query($ip)) {
14+
die('ERROR');
15+
}
16+
17+
echo '<pre>';
18+
echo 'Country Code : ' . $location->countryCode . "\n";
19+
echo 'Country Name : ' . $location->countryName . "\n";
20+
echo 'Region Name : ' . $location->regionName . "\n";
21+
echo 'City Name : ' . $location->cityName . "\n";
22+
echo 'Latitude : ' . $location->latitude . "\n";
23+
echo 'Longitude : ' . $location->longitude . "\n";
24+
echo 'ZIP Code : ' . $location->zipCode . "\n";
25+
echo 'Time Zone : ' . $location->timeZone . "\n";
26+
echo 'ISP : ' . $location->isp . "\n";
27+
echo 'Domain : ' . $location->domain . "\n";
28+
echo 'Latitude : ' . $location->netSpeed . "\n";
29+
echo 'IDD Code : ' . $location->iddCode . "\n";
30+
echo 'Area Code : ' . $location->areaCode . "\n";
31+
echo 'Weather Station Code : ' . $location->weatherStationCode . "\n";
32+
echo 'Weather Station Name : ' . $location->weatherStationName . "\n";
33+
echo 'Mobile Brand : ' . $location->mobileBrand . "\n";
34+
echo 'Elevation : ' . $location->elevation . "\n";
35+
echo 'Usage Type : ' . $location->usageType . "\n";
36+
echo '</pre>';

0 commit comments

Comments
 (0)