laravelcm / abstract-ip-geolocation
Abstract的IP地理定位API是一个快速、轻量、现代且RESTful的JSON API,可从190多个国家确定IP地址的位置和其他详细信息。
Requires
- php: ^8.2
- ext-json: *
- illuminate/support: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.15
Requires (Dev)
- laravel/pint: ^1.1
- mockery/mockery: ^1.6
- orchestra/testbench: ^8.5
- pestphp/pest: ^2.34
README
Abstract的IP地理定位API Laravel客户端库
入门指南
Abstract的IP地理定位API是一个快速、轻量、现代且RESTful的JSON API,可从190多个国家确定IP地址的位置和其他详细信息。
免费计划每月限制为1,000次请求。要启用所有数据字段和额外的请求量,请参阅https://www.abstractapi.com/api/ip-geolocation-api#pricing。
安装
该包与PHP 8兼容,并可通过Composer使用。
composer require laravelcm/abstract-ip-geolocation
使用方法
打开您的应用程序的\app\Http\Kernel.php
文件,并将以下内容添加到Kernel::middleware
属性中
protected $middleware = [ ... \Laravelcm\AbstractIpGeolocation\Middleware\AbstractIpGeolocation::class, ];
快速入门
Route::get('/', function () { $geolocation = session()->get('abstract-ip-geolocation') $location = "The IP address " . $geolocation->ipAddress; return view('index', ['location' => $location]); });
将以下字符串返回到index
视图
"The IP address 127.0.0.1"
默认情况下,此对象$geolocation
是类\Laravelcm\AbstractIpGeolocation\DataObject\GeolocationData
的实例,它通过其IP地址提供所有API转换值(数组)的对象。信息默认存储在会话中,通过中间件,这使得您可以在代码的任何地方访问这些信息。
以下是$geolocation
对象在dump后的内容
Laravelcm\AbstractIpGeolocation\DataObject\GeolocationData {#1164 +ipAddress: "166.171.248.255" +city: "Paris" +cityGeonameId: 2997712 +region: "Île-de-France" +regionIsoCode: "IDF" +regionGeonameId: 3012874 +postalCode: "75002" +country: "France" +countryCode: "FR" +countryIsEU: true +continent: "Europe" +continentCode: "EU" +continentGeonameId: 3017382 +longitude: 2.3024 +latitude: 48.6939 +security: Laravelcm\AbstractIpGeolocation\DataObject\Security {#839 +isVpn: false } +timezone: Laravelcm\AbstractIpGeolocation\DataObject\Timezone {#1182 +name: "Europe/Paris" +abbreviation: "CEST" +gmtOffset: 2 +currentTime: "19:08:34" +isDST: true } +flag: Laravelcm\AbstractIpGeolocation\DataObject\Flag {#1165 +svg: "https://static.abstractapi.com/country-flags/FR_flag.svg" +png: "https://static.abstractapi.com/country-flags/FR_flag.png" +emoji: "🇫🇷" +unicode: "U+1F1EB U+1F1F7" } +currency: Laravelcm\AbstractIpGeolocation\DataObject\Currency {#817 +name: "Euros" +code: "EUR" } +connection: Laravelcm\AbstractIpGeolocation\DataObject\Connection {#425 +connectionType: "Cable/DSL" +autonomousSystemNumber: 45980 +autonomousSystemOrganization: "Free SAS" +ispName: "ProXad network / Free SAS" +organizationName: "Proxad / Free SAS" } }
配置
配置文件位于发布提供者元素后的config/abstract-ip-geolocation.php
。
字段
默认情况下,所有字段都由Abstract API返回,但您可以选择仅从API检索您感兴趣的字段值。为此,您需要指定您想要的字段(字段列表可在以下链接中找到https://docs.abstractapi.com/ip-geolocation#request-parameters)。
/* |-------------------------------------------------------------------------- | Geolocation Fields |-------------------------------------------------------------------------- | You can include a fields value in the query parameters with a comma | separated list of the top-level keys you want to be returned. For example | "fields => 'city,region'" will return only the city and region in the response. | | see: https://docs.abstractapi.com/ip-geolocation#request-parameters */ 'fields' => null,
一旦指定了您想要的字段(例如,country,currency
),API将只返回这些值,并且在您的地理定位DTO对象中,您将只能有country
和currency
值可用 - 其他将为null。
要访问此信息,请查阅session()
。
$geolocation = session()->get('abstract-ip-geolocation'); $geolocation->country // return "France" $currency = $geolocation->currency // instance of \Laravelcm\AbstractIpGeolocation\DataObject\Currency
DTO
以下列出了可用的DTO类。在Abstract地理定位API的JSON返回中,所有对象都由DTO类表示
\Laravelcm\AbstractIpGeolocation\DataObject\GeolocationData
,它表示包含通过其IP地址与用户相关联的所有信息的地理定位类\Laravelcm\AbstractIpGeolocation\DataObject\Connection
,它表示其连接来源信息的DTO类\Laravelcm\AbstractIpGeolocation\DataObject\Currency
,它表示货币的DTO类\Laravelcm\AbstractIpGeolocation\DataObject\Flag
,它表示国家标志信息的DTO类\Laravelcm\AbstractIpGeolocation\DataObject\Timezone
,它表示时区信息的DTO类\Laravelcm\AbstractIpGeolocation\DataObject\Security
,它表示安全信息的DTO类,让您知道用户是否在使用VPN
许可证
本软件包是开源软件,遵循MIT许可协议。