mirror of
https://github.com/chinayin/ip2region-sdk-php.git
synced 2025-04-06 04:15:48 +08:00
升级2.x版本,支持xdb
This commit is contained in:
parent
2ea1b79dbd
commit
319a498086
12
.github/dependabot.yml
vendored
Normal file
12
.github/dependabot.yml
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: "github-actions"
|
||||||
|
directory: "/"
|
||||||
|
target-branch: "2.x"
|
||||||
|
schedule:
|
||||||
|
interval: "monthly"
|
||||||
|
- package-ecosystem: "composer"
|
||||||
|
directory: "/"
|
||||||
|
target-branch: "2.x"
|
||||||
|
schedule:
|
||||||
|
interval: "daily"
|
42
.github/workflows/php.yml
vendored
Normal file
42
.github/workflows/php.yml
vendored
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
name: PHP Composer
|
||||||
|
|
||||||
|
on: [ push, pull_request ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: build (PHP ${{ matrix.php-versions }})
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
php-versions: [ '7.2', '7.4', '8.0', '8.1' ]
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Setup PHP, with composer and extensions
|
||||||
|
uses: shivammathur/setup-php@v2
|
||||||
|
with:
|
||||||
|
php-version: ${{ matrix.php-versions }}
|
||||||
|
tools: pecl
|
||||||
|
extensions: mbstring, dom
|
||||||
|
|
||||||
|
- name: Get composer cache directory
|
||||||
|
id: composer-cache
|
||||||
|
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
|
||||||
|
|
||||||
|
- name: Cache composer dependencies
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: ${{ steps.composer-cache.outputs.dir }}
|
||||||
|
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
|
||||||
|
restore-keys: ${{ runner.os }}-composer-
|
||||||
|
|
||||||
|
- name: Validate composer.json and composer.lock
|
||||||
|
run: composer validate
|
||||||
|
|
||||||
|
- name: Install Composer dependencies
|
||||||
|
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader
|
||||||
|
|
||||||
|
- name: Run test suite
|
||||||
|
run: composer travis
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -5,3 +5,5 @@ Thumbs.db
|
|||||||
ehthumbs.db
|
ehthumbs.db
|
||||||
Desktop.ini
|
Desktop.ini
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
.php-cs-fixer.cache
|
||||||
|
phpunit.xml
|
||||||
|
85
README.md
85
README.md
@ -1,20 +1,81 @@
|
|||||||
### ip2region SDK for PHP
|
# ip2region SDK for PHP (定期更新数据版)
|
||||||
|
|
||||||
|
[](https://github.com/chinayin)
|
||||||
|
[](LICENSE)
|
||||||
|
[](https://packagist.org/packages/chinayin/ip2region)
|
||||||
|
[](https://packagist.org/packages/chinayin/ip2region)
|
||||||
|

|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
|
|
||||||
|
运行环境要求 PHP 7.1 及以上版本,以及[cURL](http://php.net/manual/zh/book.curl.php)。
|
||||||
|
|
||||||
|
#### 定期更新数据版
|
||||||
|
|
||||||
|
特点:`xdb数据`封装在composer包内,数据会定期更新
|
||||||
|
|
||||||
> composer require chinayin/ip2region
|
> composer require chinayin/ip2region
|
||||||
|
|
||||||
|
#### 官方原生查询包
|
||||||
|
|
||||||
|
特点:包更小,数据路径自定义
|
||||||
|
|
||||||
|
使用方法:[github.com/chinayin/ip2region-core](https://github.com/chinayin/ip2region-core-php)
|
||||||
|
|
||||||
### Quick Examples
|
### Quick Examples
|
||||||
|
|
||||||
|
#### 完全基于文件的查询
|
||||||
|
|
||||||
```php
|
```php
|
||||||
use lionsoul2014\Ip2Region;
|
use ip2region\Ip2Region;
|
||||||
|
|
||||||
$ip2region = new Ip2Region;
|
$ip = '1.2.3.4';
|
||||||
|
try {
|
||||||
$ip = '220.181.38.150';
|
$searcher = Ip2Region::newWithFileOnly();
|
||||||
$data = $ip2region->memorySearch($ip);
|
$region = $searcher->search($ip);
|
||||||
$data = $ip2region->binarySearch($ip);
|
// 或
|
||||||
$data = $ip2region->btreeSearch($ip);
|
$region = Ip2Region::search($ip);
|
||||||
|
var_dump($region);
|
||||||
// binary算法/b-tree算法/Memory算法:
|
} catch (\Exception $e) {
|
||||||
// 0.x毫秒/0.1x毫秒/0.1x毫秒
|
var_dump($e->getMessage());
|
||||||
// 任何客户端b-tree都比binary算法快,当然Memory算法固然是最快的!
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> 备注:并发使用,每个线程或者协程需要创建一个独立的 searcher 对象。
|
||||||
|
|
||||||
|
#### 缓存 VectorIndex 索引
|
||||||
|
|
||||||
|
如果你的 php 母环境支持,可以预先加载 vectorIndex 缓存,然后做成全局变量,每次创建 Searcher 的时候使用全局的
|
||||||
|
vectorIndex,可以减少一次固定的 IO 操作从而加速查询,减少 io 压力。
|
||||||
|
|
||||||
|
```php
|
||||||
|
use ip2region\Ip2Region;
|
||||||
|
|
||||||
|
$ip = '1.2.3.4';
|
||||||
|
try {
|
||||||
|
$region = Ip2Region::newWithVectorIndex()->search($ip);
|
||||||
|
var_dump($region);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
var_dump($e->getMessage());
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> 备注:并发使用,每个线程或者协程需要创建一个独立的 searcher 对象,但是都共享统一的只读 vectorIndex。
|
||||||
|
|
||||||
|
#### 缓存整个 xdb 数据
|
||||||
|
|
||||||
|
如果你的 PHP 母环境支持,可以预先加载整个 xdb 的数据到内存,这样可以实现完全基于内存的查询,类似之前的 memory search 查询。
|
||||||
|
|
||||||
|
```php
|
||||||
|
use ip2region\Ip2Region;
|
||||||
|
|
||||||
|
$ip = '1.2.3.4';
|
||||||
|
try {
|
||||||
|
$region = Ip2Region::newWithBuffer()->search($ip);
|
||||||
|
var_dump($region);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
var_dump($e->getMessage());
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> 备注:并发使用,用整个 xdb 缓存创建的 searcher 对象可以安全用于并发。
|
||||||
|
Binary file not shown.
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "chinayin/ip2region",
|
"name": "chinayin/ip2region",
|
||||||
"description": "Ip2region is a offline IP location library with accuracy rate of 99.9% and 0.0x millseconds searching performance. DB file is less then 5Mb with all ip address stored",
|
"description": "[定期更新数据版] Ip2region (2.0 - xdb) is a offline IP address manager framework and locator with ten microsecond searching performance. xdb engine implementation for many programming languages\n\n",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "lionsoul2014",
|
"name": "lionsoul2014",
|
||||||
@ -13,19 +13,32 @@
|
|||||||
],
|
],
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"require": {
|
"require": {
|
||||||
"PHP": ">=5.6"
|
"PHP": ">=7.1",
|
||||||
|
"chinayin/ip2region-core": "^2.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^6.0"
|
"phpunit/phpunit": "^6.0",
|
||||||
|
"friendsofphp/php-cs-fixer": "^3.0",
|
||||||
|
"phpstan/phpstan": "^1.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"lionsoul2014\\": "src/"
|
"ip2region\\": "src"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"lionsoul2014\\Tests\\": "tests/"
|
"ip2region\\Tests\\": "tests"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "vendor/bin/phpunit",
|
||||||
|
"test-ci": "vendor/bin/phpunit --coverage-text",
|
||||||
|
"lint": "vendor/bin/php-cs-fixer fix -v",
|
||||||
|
"analyse": "vendor/bin/phpstan analyse",
|
||||||
|
"travis": [
|
||||||
|
"composer lint",
|
||||||
|
"composer analyse"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
23
phpunit.xml
23
phpunit.xml
@ -1,23 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<phpunit backupGlobals="false"
|
|
||||||
backupStaticAttributes="false"
|
|
||||||
colors="true"
|
|
||||||
convertErrorsToExceptions="true"
|
|
||||||
convertNoticesToExceptions="true"
|
|
||||||
convertWarningsToExceptions="true"
|
|
||||||
processIsolation="false"
|
|
||||||
stopOnFailure="false"
|
|
||||||
syntaxCheck="false"
|
|
||||||
bootstrap="tests/bootstrap.php"
|
|
||||||
>
|
|
||||||
<testsuites>
|
|
||||||
<testsuite name="All">
|
|
||||||
<directory>./tests/</directory>
|
|
||||||
</testsuite>
|
|
||||||
</testsuites>
|
|
||||||
<filter>
|
|
||||||
<whitelist>
|
|
||||||
<directory suffix=".php">src/</directory>
|
|
||||||
</whitelist>
|
|
||||||
</filter>
|
|
||||||
</phpunit>
|
|
@ -20,6 +20,20 @@ class Ip2Region
|
|||||||
public const XDB_PATH = __DIR__ . '/../assets/ip2region.xdb';
|
public const XDB_PATH = __DIR__ . '/../assets/ip2region.xdb';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 完全基于文件的查询
|
||||||
|
*
|
||||||
|
* @param $ip
|
||||||
|
* @return string|null
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public static function search($ip)
|
||||||
|
{
|
||||||
|
return XdbSearcher::newWithFileOnly(self::XDB_PATH)->search($ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 完全基于文件的查询
|
||||||
|
*
|
||||||
* 备注:并发使用,每个线程或者协程需要创建一个独立的 searcher 对象。
|
* 备注:并发使用,每个线程或者协程需要创建一个独立的 searcher 对象。
|
||||||
* @return XdbSearcher
|
* @return XdbSearcher
|
||||||
*/
|
*/
|
||||||
|
@ -50,6 +50,15 @@ class Ip2RegionTest extends TestCase
|
|||||||
$this->assertEquals($expected, $r);
|
$this->assertEquals($expected, $r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSearchDefault()
|
||||||
|
{
|
||||||
|
foreach ($this->ips as $ip => $expected) {
|
||||||
|
$r = Ip2Region::search($ip);
|
||||||
|
printf("ip: %s, region: %s\n", $ip, $r);
|
||||||
|
$this->assertEquals($expected, $r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function testSearch()
|
public function testSearch()
|
||||||
{
|
{
|
||||||
foreach (['file', 'vectorIndex', 'content'] as $cachePolicy) {
|
foreach (['file', 'vectorIndex', 'content'] as $cachePolicy) {
|
||||||
|
@ -1,34 +1,38 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace lionsoul2014\Tests;
|
/*
|
||||||
|
* This file is part of the Ip2Region package.
|
||||||
|
*
|
||||||
|
* Copyright 2022 The Ip2Region Authors. All rights reserved.
|
||||||
|
* Use of this source code is governed by a Apache2.0-style
|
||||||
|
* license that can be found in the LICENSE file.
|
||||||
|
*
|
||||||
|
* @link https://github.com/chinayin/ip2region-sdk-php
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
use lionsoul2014\Ip2Region;
|
namespace ip2region\Tests;
|
||||||
|
|
||||||
|
use ip2region\Ip2Region;
|
||||||
|
|
||||||
class TestCase extends \PHPUnit\Framework\TestCase
|
class TestCase extends \PHPUnit\Framework\TestCase
|
||||||
{
|
{
|
||||||
|
public static function now()
|
||||||
public function testFeature()
|
|
||||||
{
|
{
|
||||||
$funs = [
|
return microtime(true) * 1000;
|
||||||
'binarySearch', 'memorySearch', 'btreeSearch'
|
|
||||||
];
|
|
||||||
$ip = '220.181.38.150';
|
|
||||||
$expected = [
|
|
||||||
'city_id' => 215,
|
|
||||||
'region' => '中国|0|北京|北京市|电信',
|
|
||||||
];
|
|
||||||
$ip2region = new Ip2Region;
|
|
||||||
foreach ($funs as $fn) {
|
|
||||||
$start = $this->getTime();
|
|
||||||
$data = $ip2region->$fn($ip);
|
|
||||||
$end = $this->getTime() - $start;
|
|
||||||
printf("%s (%s) ==> %s|%s in %.5f millseconds\n", $ip, $fn, $data['city_id'], $data['region'], $end);
|
|
||||||
$this->assertEquals($expected, $data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTime()
|
public function test()
|
||||||
{
|
{
|
||||||
return (microtime(true) * 1000);
|
$ip = '1.2.3.4';
|
||||||
|
try {
|
||||||
|
$region = Ip2Region::newWithFileOnly()->search($ip);
|
||||||
|
var_dump($region);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
var_dump($e->getMessage());
|
||||||
|
}
|
||||||
|
$this->assertTrue(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once __DIR__ . '/../src/Ip2Region.class.php';
|
/*
|
||||||
|
* This file is part of the Ip2Region package.
|
||||||
|
*
|
||||||
|
* Copyright 2022 The Ip2Region Authors. All rights reserved.
|
||||||
|
* Use of this source code is governed by a Apache2.0-style
|
||||||
|
* license that can be found in the LICENSE file.
|
||||||
|
*
|
||||||
|
* @link https://github.com/chinayin/ip2region-sdk-php
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user