taylornetwork / geo-ip
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ^6.2
- laravel/framework: ^5.3
This package is auto-updated.
Last update: 2024-09-24 13:55:59 UTC
README
在 Laravel 5 中获取 IP 地址位置的一种简单方法
安装
通过 Composer
$ composer require tayornetwork/geo-ip
设置
在 config/app.php 中的 providers 数组中注册服务提供者
'providers' => [ TaylorNetwork\GeoIP\GeoIPServiceProvider::class, ],
在 config/app.php 中的 aliases 数组中添加 GeoIP 门面
'aliases' => [ 'GeoIP' => TaylorNetwork\GeoIP\Facades\GeoIP::class, ],
发布配置
$ php artisan vendor:publish
将 geoip.php 添加到您的 config 文件夹
基本用法
辅助函数
此包包含一个全局辅助函数 geoip,它将为您调用 GeoIP 类。
通过传递给 geoip 获取特定 IP 地址的位置
geoip('8.8.8.8'); // Returns an object {#xxx +"ip": "8.8.8.8", +"country_code": "US", +"country_name": "United States", +"region_code": "CA", +"region_name": "California", +"city": "Mountain View", +"zip_code": "94035", +"time_zone": "America/Los_Angeles", +"latitude": 37.386, +"longitude": -122.0838, +"metro_code": 807, }
不带参数调用 geoip() 将返回您 IP 地址的位置。
类
$geoip = new TaylorNetwork\GeoIP\GeoIP ();
可用方法
findIP (string | null $ip)
用法与辅助函数相同(见辅助函数)
makeRequest (string | null $ip)
返回 HTTP 请求的原始响应。
$geoip->makeRequest('8.8.8.8'); // Returns "{"ip":"8.8.8.8","country_code":"US","country_name":"United States","region_code":"CA","region_name":"California","city":"Mountain View","zip_code":"94035","time_zone":"America/Los_Angeles","latitude":37.386,"longitude":-122.0838,"metro_code":807}\n"
如果没有参数调用,将返回您的 IP 地址位置。
decode (mixed $response)
解码 $response 给出的响应。
默认情况下返回 JSON 字符串(见 makeRequest),decode 将使用 json_decode 解码响应以返回对象。
$response = $geoip->makeRequest('8.8.8.8'); /* * We need to remove the newline at the end of the response * or decode will fail due to invalid JSON. * * By default this is done in a driver callback function. */ $trimmed = trim($response, "\n"); $geoip->decode($trimmed); // Returns {#xxx +"ip": "8.8.8.8", +"country_code": "US", +"country_name": "United States", +"region_code": "CA", +"region_name": "California", +"city": "Mountain View", +"zip_code": "94035", +"time_zone": "America/Los_Angeles", +"latitude": 37.386, +"longitude": -122.0838, +"metro_code": 807, }
getGuzzleClient ()
返回 GuzzleHttp\Client 实例
getDriver ()
返回驱动程序的实例,默认为 FreeGeoIP
见 驱动程序
门面
GeoIP::findIP()
类中的所有方法都通过门面可用。(见 类)
驱动程序
此包默认包含一个 FreeGeoIP 驱动程序。但是,您可以使用此包与任何自定义驱动程序一起使用。
要创建新驱动程序,请运行
$ php artisan geoip:driver DriverName
这将生成位于 App\GeoIP\Drivers 中的驱动程序类
要使用驱动程序,它必须在 config/geoip.php 中注册
驱动程序类可能看起来像这样。
namespace App\GeoIP\Drivers; use TaylorNetwork\GeoIP\Drivers\Driver; class DriverName extends Driver { /** * Driver Definition * * Set all required properties here. * * @return void */ public function define () { $this->name = 'DriverName'; $this->method = 'GET'; $this->URL = 'http://driver-url.com'; $this->responseType = 'JSON'; } }
define() 函数中的所有属性必须设置为非空值
覆盖方法
如果您想使用自己的函数来查找 IP 地址,或者在 HTTP 请求之后需要格式化响应,您可以在驱动程序中覆盖这些方法。
responseCallback (mixed $response)
在 HTTP 请求之后调用,但在传递给 GeoIP 解码函数之前(见 解码)
public function responseCallback($response) { // Code to format, etc. return $response; }
findIP (string | null $ip)
在从类调用 findIP 时调用。如果返回 false,则 GeoIP 将执行工作。如果不这样做,则将响应传递给 responseCallback,然后传递给 decode
public function findIP ($ip = null) { // Code to do HTTP request, and get response... return $response; }
可用方法
property (string $key)
返回驱动程序属性。
$driver->property('name'); // Returns 'DriverName'
properties ()
以关联数组返回所有驱动程序属性。
鸣谢
- 作者:Sam Taylor
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可文件