Skip to content

Commit 62ecd10

Browse files
committed
Added supports for address type and category fields.
1 parent f038e11 commit 62ecd10

File tree

10 files changed

+938
-554
lines changed

10 files changed

+938
-554
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) 2020 IP2Location.com
3+
Copyright (c) 2021 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ This module enables users to retrieve below geolocation information from an IP a
1616
* Weather Station Information
1717
* Elevation
1818
* Usage Type
19+
* Address Type
20+
* Category
1921

2022

2123
## Installation
@@ -57,6 +59,8 @@ Below are the methods supported for BIN data file lookup.
5759
$mobileCarrierName = $ipl->getMobileCarrierName($ip);
5860
$elevation = $ipl->getElevation($ip);
5961
$usageType = $ipl->getUsageType($ip);
62+
$addressType = $ipl->getAddressType($ip);
63+
$category = $ipl->getCategory($ip);
6064

6165
### Web Service
6266
Use following codes in your application for get geolocation information.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ip2location/codeigniter-ip2location",
3-
"description": "IP2Location library for CodeIgniter. Use IP2Location geolocation database to lookup the country, region, city, coordinates, zip code, time zone, ISP, domain name, connection type, area code, weather, MCC, MNC, mobile brand name, elevation and usage type that any IP address or hostname originates from.",
3+
"description": "IP2Location library for CodeIgniter. Use IP2Location geolocation database to lookup the country, region, city, coordinates, zip code, time zone, ISP, domain name, connection type, area code, weather, MCC, MNC, mobile brand name, elevation, usage type, IP address type and IAB advertising category from IP address using IP2Location database.",
44
"keywords": ["ip2location-bin-database", "geolocation-information", "codeigniter-ip2location", "ip-geolocation", "geolocation", "ip-lookup", "ip-address-database", "ip-database"],
55
"homepage": "https://www.ip2location.com",
66
"license": "MIT",

controllers/IP2Location_test.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
<?php namespace App\Controllers;
2-
3-
use App\Libraries\IP2Location_lib;
4-
1+
<?php
52
date_default_timezone_set('Etc/UTC');
63

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

libraries/IP2Location_lib.php

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

34
// BIN Database Setting
45
if(!defined('IP2LOCATION_DATABASE')) {
@@ -26,7 +27,10 @@
2627
define('IP2LOCATION_LANGUAGE', 'en');
2728
}
2829

29-
require_once('ip2location/IP2Location.php');
30+
require_once('ip2location/Country.php');
31+
require_once('ip2location/Database.php');
32+
require_once('ip2location/IpTools.php');
33+
require_once('ip2location/WebService.php');
3034

3135
class IP2Location_lib {
3236
private $database;
@@ -116,6 +120,14 @@ public function getUsageType($ip=NULL) {
116120
return self::$ip2location->lookup(self::getIP($ip), \IP2Location\Database::USAGE_TYPE);
117121
}
118122

123+
public function getAddressType($ip=NULL) {
124+
return self::$ip2location->lookup(self::getIP($ip), \IP2Location\Database::ADDRESS_TYPE);
125+
}
126+
127+
public function getCategory($ip=NULL) {
128+
return self::$ip2location->lookup(self::getIP($ip), \IP2Location\Database::CATEGORY);
129+
}
130+
119131
public function getWebService($ip=NULL) {
120132
$ws = new \IP2Location\WebService(IP2LOCATION_API_KEY, IP2LOCATION_PACKAGE, IP2LOCATION_USESSL);
121133
return $ws->lookup(self::getIP($ip), IP2LOCATION_ADDONS, IP2LOCATION_LANGUAGE);

libraries/ip2location/Country.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace IP2Location;
4+
5+
/**
6+
* Country class.
7+
*/
8+
class Country
9+
{
10+
/**
11+
* Unable to locate CSV file.
12+
*
13+
* @var int
14+
*/
15+
public const EXCEPTION_FILE_NOT_EXISTS = 10000;
16+
17+
/**
18+
* Invalid CSV file.
19+
*
20+
* @var int
21+
*/
22+
public const EXCEPTION_INVALID_CSV = 10001;
23+
24+
/**
25+
* Unable to read the CSV file.
26+
*
27+
* @var int
28+
*/
29+
public const EXCEPTION_UNABLE_TO_OPEN_CSV = 10002;
30+
31+
/**
32+
* No record found.
33+
*
34+
* @var int
35+
*/
36+
public const EXCEPTION_NO_RECORD = 10003;
37+
38+
/**
39+
* Fields from CSV.
40+
*
41+
* @var array
42+
*/
43+
protected $fields = [];
44+
45+
/**
46+
* Records from CSV.
47+
*
48+
* @var array
49+
*/
50+
protected $records = [];
51+
52+
/**
53+
* Constructor.
54+
*
55+
* @param string $csv Path to CSV file
56+
*
57+
* @throws \Exception
58+
*/
59+
public function __construct($csv)
60+
{
61+
if (!file_exists($csv)) {
62+
throw new \Exception(__CLASS__ . ': The CSV file "' . $csv . '" is not found.', self::EXCEPTION_FILE_NOT_EXISTS);
63+
}
64+
65+
$file = fopen($csv, 'r');
66+
67+
if (!$file) {
68+
throw new \Exception(__CLASS__ . ': Unable to read "' . $csv . '".', self::EXCEPTION_UNABLE_TO_OPEN_CSV);
69+
}
70+
71+
$line = 1;
72+
73+
while (!feof($file)) {
74+
$data = fgetcsv($file);
75+
76+
if (!$data) {
77+
++$line;
78+
continue;
79+
}
80+
81+
// Parse the CSV fields
82+
if ($line == 1) {
83+
if ($data[0] != 'country_code') {
84+
throw new \Exception(__CLASS__ . ': Invalid country information CSV file.', self::EXCEPTION_INVALID_CSV);
85+
}
86+
87+
$this->fields = $data;
88+
} else {
89+
$this->records[$data[0]] = $data;
90+
}
91+
92+
++$line;
93+
}
94+
}
95+
96+
/**
97+
* Get the country information.
98+
*
99+
* @param string $countryCode The country ISO 3166 country code.
100+
*
101+
* @throws \Exception
102+
*
103+
* @return array
104+
*/
105+
public function getCountryInfo($countryCode = '')
106+
{
107+
if (empty($this->records)) {
108+
throw new \Exception(__CLASS__ . ': No record available.', self::EXCEPTION_NO_RECORD);
109+
}
110+
111+
$results = [];
112+
113+
if ($countryCode) {
114+
if (!isset($this->records[$countryCode])) {
115+
return [];
116+
}
117+
118+
for ($i = 0; $i < \count($this->fields); ++$i) {
119+
$results[$this->fields[$i]] = $this->records[$countryCode][$i];
120+
}
121+
122+
return $results;
123+
}
124+
125+
foreach ($this->records as $record) {
126+
$data = [];
127+
128+
for ($i = 0; $i < \count($this->fields); ++$i) {
129+
$data[$this->fields[$i]] = $record[$i];
130+
}
131+
132+
$results[] = $data;
133+
}
134+
135+
return $results;
136+
}
137+
}

0 commit comments

Comments
 (0)