This package provides an API for the IPIP.NET databases. The API only works with the DATX databases.
We recommend installing this package with Composer.
To download Composer, run in the root directory of your project:
curl -sS https://getcomposer.org/installer | php
You should now have the file composer.phar
in your project directory.
Run in your project root:
php composer.phar require metowolf/ip2
You should now have the files composer.json
and composer.lock
as well as the directory vendor in your project directory. If you use a version control system, composer.json
should be added to it.
After installing the dependencies, you need to require the Composer autoloader from your code:
require 'vendor/autoload.php';
To use this library, you should download DATX database via IPIP.NET and put it to your project system anywhere, or just use the free database in src/17monipdb.datx
.
See the Example for more details.
<?php
require 'vendor/autoload.php';
use Ip2\Db\Reader;
$reader = new Reader();
// $reader = new Reader('/usr/local/share/IPIP.datx');
$reader->find('101.6.6.6');
print($reader->country() . "\n"); // '**'
print($reader->state() . "\n"); // '北京'
print($reader->city() . "\n"); // '北京'
print($reader->org() . "\n"); // '清华大学'
print($reader->isp() . "\n"); // '教育网'
print($reader->isocode() . "\n"); // 'CN'
// print_r($reader->range($TOKEN)); // ['101.6.0.0', '101.6.255.255']
print_r($reader->cidrs()); // ['101.6.0.0/16']
print($reader->china_code() . "\n"); // 110000
print($reader->version() . "\n"); // '2018070201'
be compatible to ipip/datx
<?php
require_once __DIR__ . '/vendor/autoload.php';
$bs = new Ip2\Db\Reader("/path/to/mydata4vipday4.datx"); // 城市级数据库
var_export($bs->find("223.220.233.0"));
method | 中文含义 | database |
---|---|---|
country() | 国家 | free edition |
state() | 省会或直辖市 | free edition |
city() | 地区或城市 | free edition |
org() | 学校或单位 | free edition |
isp() | 运营商字段 | premium weekly edition |
latitude() | 纬度 | daily edition |
longitude() | 经度 | daily edition |
timezone() | 时区一 | daily edition |
timezone2() | 时区二 | daily edition |
china_code() | **行政区划代码(support fake function) | daily edition |
phone_prefix() | 国际电话代码 | daily edition |
isocode() | 国家二位代码(support fake function) | daily edition |
continent() | 世界大洲代码 | daily edition |
is_idc() | IDC标签 | ultimate edition |
is_base_station() | 基站标签 | ultimate edition |
由于 range 接口可能允许用户对 datx 加密文件格式进行循环反解,这是违反 IPIP.net 的用户协议的,在 IPIP.net 方面建议下,调用该接口需要同时传递一个字符串 Token 方可正常使用。
Token 获取流程如下:
- 向 IPIP.net 技术支持邮箱发送授权申请书,内容需包括使用人、使用主体、使用场景等;
- 收到 IPIP.net 方面同意回复后,转发该邮件至
[email protected]
; - 我们会在三个工作日内审核并将 Token 发送给你;
Test | ipip/datx (original) | metowolf/IP2 |
---|---|---|
1k random IP | ~ 159285 rps | ~ 215789 rps or 1.35 times faster |
10k random IP | ~ 144778 rps | ~ 205389 rps or 1.41 times faster |
100k random IP | ~ 157085 rps | ~ 214852 rps or 1.36 times faster |
100k static IP 216.243.15.197 |
~ 108680 rps | ~ 231163 rps or 2.12 times faster |
This library requires PHP 7.0 or greater. This library maybe compatible with 5.6.\
IP2 © metowolf, Released under the MIT License.
Authored and maintained by metowolf with help from contributors (@kn007, @akafeng)
Blog @metowolf · GitHub @metowolf · Twitter @metowolf · Telegram Channel @metooooo