Skip to content

Commit e3c4e56

Browse files
committed
Add IP2Location web service
1 parent 03104e7 commit e3c4e56

File tree

5 files changed

+2336
-1969
lines changed

5 files changed

+2336
-1969
lines changed

LICENSE.TXT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 IP2Location.com
3+
Copyright (c) 2020 IP2Location.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 69 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CodeIgniter IP2Location Library
22
===============================
3-
43
This module enables users to retrieve below geolocation information from an IP address. It supports both the IPv4 and IPv6 address.
54

65
* Country
@@ -19,61 +18,84 @@ This module enables users to retrieve below geolocation information from an IP a
1918
* Usage Type
2019

2120

22-
Installation
23-
------------
21+
## Installation
2422
Upload `controllers` and `libraries` to CodeIgniter `application` folder.
2523

26-
IP2Location BIN Database
27-
------------------------
28-
This module requires IP2Location BIN database to function. An outdated BIN database was provided in this release for your testing, but it's recommended to download the latest BIN database at the below link
29-
* IP2Location LITE BIN Database (free): https://lite.ip2location.com
30-
* IP2Location BIN Database (commercial version with high accuracy): https://www.ip2location.com
24+
## Usage
25+
This module is able to query the IP address information from either BIN database or web service. This section will explain how to use this extension to query from BIN database and web service.
3126

32-
For the BIN database update, you can just rename the downloaded BIN database to *IP2LOCATION-DB.BIN* and replace the copy in *application/libraries/ip2location/* (if you didn't change the default IP2LOCATION_DATABASE constant as described in the below section).
27+
Sample codes are given in this project, under **controllers** folder. You may run the sample code by using <your_domain>/index.php/ip2location_test.
3328

34-
IPv4 BIN vs IPv6 BIN
35-
------------------------
36-
Use the IPv4 BIN file if you just need to query IPv4 addresses.
29+
### BIN Database
30+
Use following codes in your application for get geolocation information.
31+
32+
// (optional) Define IP2Location database path. By default, the IP2LOCATION_DATABASE is pointed to *application/libraries/ip2location/IP2LOCATION-DB.BIN* if you choose not to change the original settings.
33+
define('IP2LOCATION_DATABASE', '/path/to/ip2location/database');
3734

38-
Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.
35+
$ipl = new IP2Location_lib();
36+
$countryCode = $ipl->getCountryCode('8.8.8.8');
3937

40-
Usage
41-
-----
38+
Below are the methods supported for BIN data file lookup.
39+
40+
$countryCode = $ipl->getCountryCode($ip);
41+
$countryName = $ipl->getCountryName($ip);
42+
$regionName = $ipl->getRegionName($ip);
43+
$cityName = $ipl->getCityName($ip);
44+
$latitude = $ipl->getLatitude($ip);
45+
$longitude = $ipl->getLongitude($ip);
46+
$isp = $ipl->getISP($ip);
47+
$domainName = $ipl->getDomainName($ip);
48+
$zipCode = $ipl->getZIPCode($ip);
49+
$timeZone = $ipl->getTimeZone($ip);
50+
$netSpeed = $ipl->getNetSpeed($ip);
51+
$iddCode = $ipl->getIDDCode($ip);
52+
$areaCode = $ipl->getAreaCode($ip);
53+
$weatherStationCode = $ipl->getWeatherStationCode($ip);
54+
$weatherStationName = $ipl->getWeatherStationName($ip);
55+
$mcc = $ipl->getMCC($ip);
56+
$mnc = $ipl->getMNC($ip);
57+
$mobileCarrierName = $ipl->getMobileCarrierName($ip);
58+
$elevation = $ipl->getElevation($ip);
59+
$usageType = $ipl->getUsageType($ip);
60+
61+
### Web Service
4262
Use following codes in your application for get geolocation information.
4363

44-
// Define IP2Location database path (optional). By default, the IP2LOCATION_DATABASE is pointed to *application/libraries/ip2location/IP2LOCATION-DB.BIN* if you choose not to change the original settings.
45-
define('IP2LOCATION_DATABASE', '/path/to/ip2location/database');
64+
// (required) Define IP2Location API key.
65+
define('IP2LOCATION_API_KEY', 'your_api_key');
4666

47-
// Load the IP2Location library and perform the country code lookup
48-
$this->load->library('ip2location_lib');
49-
$countryCode = $this->ip2location_lib->getCountryCode('8.8.8.8');
67+
// (required) Define IP2Location Web service package of different granularity of return information.
68+
define('IP2LOCATION_PACKAGE', 'WS1');
5069

51-
Sample Code
52-
-----------
53-
Sample codes are given in this project, under **controllers** folder. You may run the sample code by using <your_domain>/index.php/ip2location_test.
70+
// (optional) Define to use https or http.
71+
define('IP2LOCATION_USESSL', false);
72+
73+
// (optional) Define extra information in addition to the above-selected package. Refer to https://www.ip2location.com/web-service/ip2location for the list of available addons.
74+
define('IP2LOCATION_ADDONS', []);
75+
76+
// (optional) Define Translation information. Refer to https://www.ip2location.com/web-service/ip2location for available languages.
77+
define('IP2LOCATION_LANGUAGE', 'zh-cn');
78+
79+
$ipl = new IP2Location_lib();
80+
$countryCode = $ipl->getWebService('8.8.8.8');
81+
82+
## Dependencies
83+
This module requires IP2Location BIN data file or IP2Location API key to function. You may download the BIN data file at
84+
85+
* IP2Location LITE BIN Data (Free): https://lite.ip2location.com
86+
* IP2Location Commercial BIN Data (Comprehensive): https://www.ip2location.com
87+
88+
An outdated BIN database was provided in this release for your testing. You are recommended to visit the above links to download the latest BIN database.
89+
90+
For the BIN database update, you can just rename the downloaded BIN database to *IP2LOCATION-DB.BIN* and replace the copy in *application/libraries/ip2location/* (if you didn't change the default IP2LOCATION_DATABASE constant as described in the Usage section).
91+
92+
You can also sign up for [IP2Location Web Service](https://www.ip2location.com/web-service/ip2location) to get one free API key.
93+
94+
## IPv4 BIN vs IPv6 BIN
95+
* Use the IPv4 BIN file if you just need to query IPv4 addresses.
96+
* Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.
5497

55-
Methods
56-
-------
57-
Below are the methods supported.
58-
59-
$countryCode = $this->ip2location_lib->getCountryCode($ip);
60-
$countryName = $this->ip2location_lib->getCountryName($ip);
61-
$regionName = $this->ip2location_lib->getRegionName($ip);
62-
$cityName = $this->ip2location_lib->getCityName($ip);
63-
$latitude = $this->ip2location_lib->getLatitude($ip);
64-
$longitude = $this->ip2location_lib->getLongitude($ip);
65-
$isp = $this->ip2location_lib->getISP($ip);
66-
$domainName = $this->ip2location_lib->getDomainName($ip);
67-
$zipCode = $this->ip2location_lib->getZIPCode($ip);
68-
$timeZone = $this->ip2location_lib->getTimeZone($ip);
69-
$netSpeed = $this->ip2location_lib->getNetSpeed($ip);
70-
$iddCode = $this->ip2location_lib->getIDDCode($ip);
71-
$areaCode = $this->ip2location_lib->getAreaCode($ip);
72-
$weatherStationCode = $this->ip2location_lib->getWeatherStationCode($ip);
73-
$weatherStationName = $this->ip2location_lib->getWeatherStationName($ip);
74-
$mcc = $this->ip2location_lib->getMCC($ip);
75-
$mnc = $this->ip2location_lib->getMNC($ip);
76-
$mobileCarrierName = $this->ip2location_lib->getMobileCarrierName($ip);
77-
$elevation = $this->ip2location_lib->getElevation($ip);
78-
$usageType = $this->ip2location_lib->getUsageType($ip);
98+
## SUPPORT
99+
79100

101+
Website: https://www.ip2location.com

controllers/IP2Location_test.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
<?php
1+
<?php namespace App\Controllers;
2+
3+
use App\Libraries\IP2Location_lib;
4+
25
date_default_timezone_set('Etc/UTC');
36

4-
class IP2Location_test extends CI_Controller {
5-
function __construct() {
6-
parent::__construct();
7-
$this->load->library('ip2location_lib');
8-
}
7+
class IP2Location_test extends BaseController {
8+
public function index() {
9+
$ipl = new IP2Location_lib();
910

10-
function index() {
11-
$countryCode = $this->ip2location_lib->getCountryCode('8.8.8.8');
11+
// BIN Database
12+
$countryCode = $ipl->getCountryCode('8.8.8.8');
1213

13-
echo '<p>Country code for 8.8.8.8: ' . $countryCode . '</p>';
14-
15-
echo '
16-
<div>You can download the latest BIN database at
17-
<ul>
18-
<li><a href="https://lite.ip2location.com">IP2Location LITE BIN Database (Free)</a></li>
19-
<li><a href="https://www.ip2location.com">IP2Location BIN Database (Comprehensive)</a></li>
20-
</ul>
21-
</div>';
14+
echo '<p>Country code for 8.8.8.8: ' . $countryCode . '</p>';
15+
16+
echo '
17+
<div>You can download the latest BIN database at
18+
<ul>
19+
<li><a href="https://lite.ip2location.com">IP2Location LITE BIN Database (Free)</a></li>
20+
<li><a href="https://www.ip2location.com">IP2Location BIN Database (Comprehensive)</a></li>
21+
</ul>
22+
</div>';
23+
24+
// Web Service
25+
echo '<pre>';
26+
print_r ($ipl->getWebService('8.8.8.8'));
27+
echo '</pre>';
2228
}
2329
}
24-

libraries/IP2Location_lib.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1-
<?php
2-
(defined('BASEPATH') || defined('SYSPATH')) or die('No direct access allowed.');
1+
<?php namespace App\Libraries;
32

3+
// BIN Database Setting
44
if(!defined('IP2LOCATION_DATABASE')) {
55
define('IP2LOCATION_DATABASE', dirname(__FILE__) . '/ip2location/IP2LOCATION-DB.BIN');
66
}
77

8+
// Web Service Settings
9+
if(!defined('IP2LOCATION_API_KEY')) {
10+
define('IP2LOCATION_API_KEY', 'demo');
11+
}
12+
13+
if(!defined('IP2LOCATION_PACKAGE')) {
14+
define('IP2LOCATION_PACKAGE', 'WS1');
15+
}
16+
17+
if(!defined('IP2LOCATION_USESSL')) {
18+
define('IP2LOCATION_USESSL', false);
19+
}
20+
21+
if(!defined('IP2LOCATION_ADDONS')) {
22+
define('IP2LOCATION_ADDONS', []);
23+
}
24+
25+
if(!defined('IP2LOCATION_LANGUAGE')) {
26+
define('IP2LOCATION_LANGUAGE', 'en');
27+
}
28+
829
require_once('ip2location/IP2Location.php');
930

1031
class IP2Location_lib {
@@ -95,6 +116,11 @@ public function getUsageType($ip=NULL) {
95116
return self::$ip2location->lookup(self::getIP($ip), \IP2Location\Database::USAGE_TYPE);
96117
}
97118

119+
public function getWebService($ip=NULL) {
120+
$ws = new \IP2Location\WebService(IP2LOCATION_API_KEY, IP2LOCATION_PACKAGE, IP2LOCATION_USESSL);
121+
return $ws->lookup(self::getIP($ip), IP2LOCATION_ADDONS, IP2LOCATION_LANGUAGE);
122+
}
123+
98124
protected function getIP($ip=NULL) {
99125
return ($ip) ? $ip : $_SERVER['REMOTE_ADDR'];
100126
}

0 commit comments

Comments
 (0)