farit-slv / sypex-geo-bundle
SypexGeo库的Symfony Bundle系统适配版本
2.0.3
2023-01-10 14:57 UTC
Requires
- php: ^7.4|^8.2
- ext-zip: *
- farit-slv/sypex-geo: ^1.1
- guzzlehttp/guzzle: ^6.0
- symfony/framework-bundle: ^3.4|^4.2|^5.1|^6.1
Requires (Dev)
- phpunit/phpunit: ^8.3
- symfony/console: ^3.4|^4.2|^5.1|^6.1
README
这是Sypex Geo Library(GitHub链接)的Symfony适配版本。
安装
步骤 1: 使用composer下载SypexGeoBundle
运行以下命令添加SypexGeoBundle
$ composer require farit-slv/sypex-geo-bundle:^2.0
步骤 2: 启用bundle
在kernel中启用bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Yamilovs\Bundle\SypexGeoBundle\SypexGeoBundle(), ); }
步骤 3: 添加配置
# app/config/config.yml faritslv_sypex_geo: mode: FILE # FILE (default) | BATCH | MEMORY database_path: "%kernel.root_dir%/../var/SypexGeoDatabase/SxGeoCity.dat"
如果需要数据库更新的代理配置,可以添加
faritslv_sypex_geo: ...... connection: proxy: host: 'xxx.xxx.xxx.xxx' port: # port number # You can enable user credentials if you have them auth: user: 'your username' password: 'your password'
步骤 4: 下载必要的数据库
将必要的数据库下载到database_path
。
- 你可以运行
php bin/console faritslv:sypex-geo:update-database-file
- 或者从Sypex Geo City手动下载
使用方法
在你的控制器中
<?php // src/Controller/FooController.php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Yamilovs\SypexGeo\SypexGeo; class FooController extends Controller { public function fooAction(Request $request, SypexGeo $sypexGeo) { $userIp = $request->getClientIp(); $testIp = '88.86.218.24'; $city = $sypexGeo->getCity($testIp, true); dump($city); } }
注意
你的本地IP地址是127.0.0.1,Sypex Geo无法获取你的城市或国家信息!
如果你想检查特定IP地址的数据
你可以运行php bin/console yamilovs:sypex-geo:get-ip-data aa.bb.cc.dd