├── README.md ├── composer.json ├── config └── config.php └── src ├── Console ├── BaseStationFindCommand.php ├── CityFindCommand.php ├── DistrictFindCommand.php └── DownloadCommand.php ├── Facades ├── BaseStation.php ├── City.php └── District.php ├── IpipServiceProvider.php └── helpers.php /README.md: -------------------------------------------------------------------------------- 1 | # laravel-ipip 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/huangdijia/laravel-ipip/version.png)](https://packagist.org/packages/huangdijia/laravel-ipip) 4 | [![Total Downloads](https://poser.pugx.org/huangdijia/laravel-ipip/d/total.png)](https://packagist.org/packages/huangdijia/laravel-ipip) 5 | 6 | ## Requirements 7 | 8 | * PHP >= 7.0 9 | * Laravel >= 5.5 10 | 11 | ## Installation 12 | 13 | First, install laravel 5.5, and make sure that the database connection settings are correct. 14 | 15 | ~~~bash 16 | composer require huangdijia/laravel-ipip 17 | ~~~ 18 | 19 | Then run these commands to publish config 20 | 21 | ~~~bash 22 | php artisan vendor:publish --provider="Huangdijia\Ipip\IpipServiceProvider" 23 | ~~~ 24 | 25 | ## Configurations 26 | 27 | ~~~php 28 | // config/ipip.php 29 | 'datx' => [ 30 | 'city' => 'path/mydata4vipday4.datx', 31 | 'district' => 'path/quxian.datx', 32 | 'basestation' => 'path/station_ip.datx', 33 | ], 34 | 'auth' => [ 35 | 'mail' => '', // mail of ipip.net 36 | 'pass' => '', // password of ipip.net 37 | ] 38 | ~~~ 39 | 40 | ## Usage 41 | 42 | ### As Facade 43 | 44 | ~~~php 45 | use Huangdijia\Ipip\Facades\BaseStation; 46 | use Huangdijia\Ipip\Facades\City; 47 | use Huangdijia\Ipip\Facades\District; 48 | 49 | ... 50 | 51 | BaseStation::find('66.249.69.48'); // ['美国', '美国', '', ''] 52 | City::find('66.249.69.48'); 53 | District::find('66.249.69.48'); 54 | 55 | ~~~ 56 | 57 | ### As Command 58 | 59 | ~~~bash 60 | php artisan ipip:basestation '66.249.69.48' # ['美国', '美国', '', ''] 61 | php artisan ipip:city '66.249.69.48' 62 | php artisan ipip:district '66.249.69.48' 63 | php artisan ipip:download # must set mail and pass at config/ipip.php 64 | ~~~ 65 | 66 | ### As Helper 67 | 68 | ~~~php 69 | ipip('city')->find('66.249.69.48'); // ['美国', '美国', '', ''] 70 | ipip_city()->find('66.249.69.48'); // ['美国', '美国', '', ''] 71 | ipip_city('66.249.69.48'); // ['美国', '美国', '', ''] 72 | 73 | ipip('basestation')->find('66.249.69.48'); 74 | ipip_basestation()->find('66.249.69.48'); 75 | ipip_basestation('66.249.69.48'); 76 | 77 | ipip('distric')->find('66.249.69.48'); 78 | ipip_distric()->find('66.249.69.48'); 79 | ipip_distric('66.249.69.48'); 80 | 81 | ~~~ 82 | 83 | ## Other 84 | 85 | > * https://www.ipip.net 86 | > * https://github.com/ipipdotnet/datx-php 87 | 88 | ## License 89 | 90 | laravel-ipip is licensed under The MIT License (MIT). -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "huangdijia/laravel-ipip", 3 | "description": "ipip for laravel", 4 | "type": "library", 5 | "keywords": [ 6 | "laravel", 7 | "ipip" 8 | ], 9 | "homepage": "https://github.com/huangdijia/laravel-ipip", 10 | "license": "MIT", 11 | "authors": [{ 12 | "name": "huangdijia", 13 | "email": "huangdijia@gmail.com" 14 | }], 15 | "require": { 16 | "php": ">=7.0.0", 17 | "illuminate/support": "^5.5|^6.0|^7.0|^8.0", 18 | "illuminate/console": "^5.5|^6.0|^7.0|^8.0", 19 | "ipip/datx": "~0.2" 20 | }, 21 | "autoload": { 22 | "files": [ 23 | "src/helpers.php" 24 | ], 25 | "psr-4": { 26 | "Huangdijia\\Ipip\\": "src/" 27 | } 28 | }, 29 | "extra": { 30 | "laravel": { 31 | "providers": [ 32 | "Huangdijia\\Ipip\\IpipServiceProvider" 33 | ], 34 | "aliases": { 35 | "BaseStation": "Huangdijia\\Ipip\\Facades\\BaseStation", 36 | "City": "Huangdijia\\Ipip\\Facades\\City", 37 | "District": "Huangdijia\\Ipip\\Facades\\District" 38 | } 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /config/config.php: -------------------------------------------------------------------------------- 1 | [ 4 | 'city' => storage_path('app/public/17monipdb/17monipdb.datx'), 5 | 'district' => '', 6 | 'basestation' => '', 7 | ], 8 | 'auth' => [ 9 | 'mail' => env('IPIP_MAIL', ''), 10 | 'pass' => env('IPIP_PASS', ''), 11 | ] 12 | ]; 13 | -------------------------------------------------------------------------------- /src/Console/BaseStationFindCommand.php: -------------------------------------------------------------------------------- 1 | argument('ip'); 15 | if (empty($ip)) { 16 | $this->error("argument ip is empty"); 17 | } 18 | $info = BaseStation::find($ip); 19 | $this->info(json_encode($info, JSON_UNESCAPED_UNICODE)); 20 | } 21 | } -------------------------------------------------------------------------------- /src/Console/CityFindCommand.php: -------------------------------------------------------------------------------- 1 | argument('ip'); 15 | if (empty($ip)) { 16 | $this->error("argument ip is empty"); 17 | } 18 | $info = City::find($ip); 19 | $this->info(json_encode($info, JSON_UNESCAPED_UNICODE)); 20 | } 21 | } -------------------------------------------------------------------------------- /src/Console/DistrictFindCommand.php: -------------------------------------------------------------------------------- 1 | argument('ip'); 15 | if (empty($ip)) { 16 | $this->error("argument ip is empty"); 17 | } 18 | $info = District::find($ip); 19 | $this->info(json_encode($info, JSON_UNESCAPED_UNICODE)); 20 | } 21 | } -------------------------------------------------------------------------------- /src/Console/DownloadCommand.php: -------------------------------------------------------------------------------- 1 | cookiejar = storage_path('app/public/ipip.cookie'); 21 | $zip_file = storage_path('app/public/17monipdb.zip'); 22 | $datx_file = storage_path('app/public/17monipdb/17monipdb.datx'); 23 | 24 | if (!config('ipip.auth.mail') || !config('ipip.auth.pass')) { 25 | throw new Exception('Please set ipip.auth.mail && ipip.auth.pass'); 26 | return; 27 | } 28 | 29 | // login ipip 30 | $this->info('logining ipip.net'); 31 | $this->login( 32 | config('ipip.auth.mail'), 33 | config('ipip.auth.pass') 34 | ); 35 | $this->info('logined'); 36 | 37 | // downling 38 | $this->info('downloading'); 39 | $this->download($zip_file); 40 | if (!is_file($zip_file)) { 41 | throw new Exception('Download faild!'); 42 | } 43 | $this->info('download success! path: ' . $zip_file); 44 | 45 | // unzip 46 | $this->info('extracting'); 47 | $this->extract($zip_file); 48 | if (!is_file($datx_file)) { 49 | throw new Exception("Extract faild", 1); 50 | } 51 | $this->info('extracted success! path:' . $datx_file); 52 | } 53 | 54 | private function login($mail = '', $pass = '') 55 | { 56 | $data = http_build_query([ 57 | 'csrf' => '', 58 | 'mail' => $mail, 59 | 'pass' => $pass, 60 | ]); 61 | $ch = curl_init(self::IPIP_LOGIN_URL); 62 | curl_setopt($ch, CURLOPT_HEADER, 0); 63 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 64 | curl_setopt($ch, CURLOPT_MAXREDIRS, 1); 65 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 66 | curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 67 | curl_setopt($ch, CURLOPT_POST, 1); 68 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 69 | curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiejar); 70 | $response = curl_exec($ch); 71 | if (curl_errno($ch)) { 72 | throw new Exception(curl_error($ch)); 73 | } 74 | if ( 75 | stripos($response, '请使用邮箱登录') 76 | || stripos($response, '登录失败') 77 | ) { 78 | throw new Exception('Login faild'); 79 | } 80 | curl_close($ch); 81 | } 82 | 83 | private function download($zip_file) 84 | { 85 | $fp = fopen($zip_file, 'w+'); 86 | $ch = curl_init(self::IPIP_DOWNLOAD_URL); 87 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 88 | curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiejar); 89 | curl_setopt($ch, CURLOPT_FILE, $fp); 90 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 91 | curl_exec($ch); 92 | if (curl_errno($ch)) { 93 | throw new Exception(curl_error($ch)); 94 | } 95 | fclose($fp); 96 | } 97 | 98 | private function extract($zip_file) 99 | { 100 | // unzip 101 | try { 102 | $zip = new ZipArchive; 103 | if ($zip->open($zip_file) === true) { 104 | $zip->extractTo(storage_path('app/public/17monipdb')); 105 | $zip->close(); 106 | } 107 | } catch (Exception $e) { 108 | throw $e; 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/Facades/BaseStation.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 27 | $this->publishes([__DIR__ . '/../config/config.php' => $this->app->basePath('config/ipip.php')]); 28 | } 29 | } 30 | 31 | public function register() 32 | { 33 | $this->app->singleton(City::class, function () { 34 | $path = config('ipip.datx.city'); 35 | return new City($path); 36 | }); 37 | $this->app->alias(City::class, 'ipip.city'); 38 | 39 | $this->app->singleton(District::class, function () { 40 | $path = config('ipip.datx.district'); 41 | return new District($path); 42 | }); 43 | $this->app->alias(District::class, 'ipip.district'); 44 | 45 | $this->app->singleton(BaseStation::class, function () { 46 | $path = config('ipip.datx.basestation'); 47 | return new BaseStation($path); 48 | }); 49 | $this->app->alias(BaseStation::class, 'ipip.basestation'); 50 | 51 | $this->commands($this->commands); 52 | } 53 | 54 | public function provides() 55 | { 56 | return [ 57 | City::class, 58 | 'ipip.city', 59 | 60 | District::class, 61 | 'ipip.district', 62 | 63 | BaseStation::class, 64 | 'ipip.basestation', 65 | ]; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | find($ip); 18 | } 19 | } 20 | 21 | if (!function_exists('ipip_district')) { 22 | function ipip_district($ip = null) 23 | { 24 | if (is_null($ip)) { 25 | return app('ipip.district'); 26 | } 27 | return app('ipip.district')->find($ip); 28 | } 29 | } 30 | 31 | if (!function_exists('ipip_base_station')) { 32 | function ipip_base_station($ip = null) 33 | { 34 | if (is_null($ip)) { 35 | return app('ipip.basestation'); 36 | } 37 | return app('ipip.basestation')->find($ip); 38 | } 39 | } 40 | 41 | --------------------------------------------------------------------------------