black-sheep-tech / laravel-ip-api
IP API服务的简单服务提供程序/包装器(https://ip-api.com)。
v1.0.0
2024-09-04 20:46 UTC
Requires
- php: ^8.1
- archtechx/enums: ^1.1
- illuminate/config: ^10.48.4|^11.0.8
- illuminate/console: ^10.48.4|^11.0.8
- illuminate/contracts: ^10.48.4|^11.0.8
- illuminate/http: ^10.48.4|^11.0.8
- illuminate/support: ^10.48.4|^11.0.8
- symfony/console: ^6.0|^7.0
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.17
- nunomaduro/phpinsights: ^2.11
- pestphp/pest: ^2.35
- pestphp/pest-plugin-laravel: ^2.4
- pestphp/pest-plugin-stressless: ^2.2
- pestphp/pest-plugin-type-coverage: ^2.8
This package is auto-updated.
Last update: 2024-09-04 20:59:30 UTC
README
Laravel IpApi 是一个针对Laravel的包,它提供了一个使用IpApi API轻松获取IP地址信息的简便方法。
安装
您可以通过composer安装此包
composer require black-sheep-tech/laravel-ip-api
通用配置
此包开箱即用,但您可以按需进行自定义。
动态配置
// Set API Key fluently $info = IpApi::geolocation()->apiKey('yourapikeyhere')->query('google.com')->get(); // Set Base URL fluently $info = IpApi::geolocation()->baseUrl('http://ip-api.com/')->query('google.com')->get();
使用环境变量
您可以在.env
文件中设置以下环境变量
IP_API_BASE_URL=http://ip-api.com/ IP_API_API_KEY=your_api_key IP_API_DEFAULT_QUERY=google.com IP_API_DEFAULT_LANG=en IP_API_DEFAULT_FORMAT=json IP_API_DEFAULT_FIELDS=country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,query
配置文件
为了更细致的配置,您可以通过运行以下命令将配置文件发布到项目中
php artisan vendor:publish --provider="BlackSheepTech\IpApi\IpApiServiceProvider"
这将创建一个ip-api.php
文件在您的config
目录中,您可以在此处自定义包配置以符合您的喜好。
过度使用保护
该包内置了过度使用保护功能,可防止您对API进行过多请求而被暂时禁止。但是,当然,您可以通过将“IP_API_OVERUSAGE_PROTECTION”环境变量设置为false来禁用此功能。
IP_API_OVERUSAGE_PROTECTION=false
也可以动态禁用
$info = IpApi::geolocation()->disableOverusageProtection()->query('google.com')->get();
用法
该包提供对地理定位和批量API的访问。
地理定位API
- 基本用法
use BlackSheepTech\IpApi\IpApi; $info = IpApi::geolocation()->query('google.com')->get();
- 高级用法
//Return Format - can be json, xml, csv, line or php $info = IpApi::geolocation()->query('google.com')->format('json')->get(); //Return Fields - Supported fields can be found at https://ip-api.com/docs/api:json $info = IpApi::geolocation()->query('google.com')->fields('countryCode,lat,lon,timezone,query')->get(); // or $info = IpApi::geolocation()->query('google.com')->fields(['countryCode', 'lat', 'lon', 'timezone', 'query'])->get(); //Return Language - Supported languages can be found at https://ip-api.com/docs/api:json $info = IpApi::geolocation()->query('google.com')->language('es')->get();
- 以对象形式返回
您可以通过以下方式获取响应对象
$info = IpApi::geolocation()->query('google.com')->get(true); //Or $info = IpApi::geolocation()->query('google.com')->getAsObject(); //When using object return, the format provided is disregarded. $info = IpApi::geolocation()->query('google.com')->format('php')->getAsObject(); //->format('php') will be ignored and have no impact on the response.
批量API
- 基本用法
use BlackSheepTech\IpApi\IpApi; $entities = [ { "query": "google.com" },{ "query": "facebook.com" } ]; $info = IpApi::batch()->entities($entities)->get();
- 自定义返回
use BlackSheepTech\IpApi\IpApi; $entities = [ { "query": "google.com", "fields": "country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,query", "lang": "en", },{ "query": "facebook.com", "fields": "country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,query", "lang": "en", } ]; $info = IpApi::batch()->entities($entities)->get();
- 以对象形式返回
您可以通过以下方式获取响应对象
$entities = [ { "query": "google.com" },{ "query": "facebook.com" } ]; $info = IpApi::batch()->entities($entities)->get(true); //Or $info = IpApi::batch()->entities($entities)->getAsObject();
要求
- PHP 8.0或更高版本
- Laravel框架版本9.0或更高版本
许可
此包是开源软件,受MIT许可许可。
贡献
欢迎贡献!请随时在GitHub上提交Pull Request。