xbnz / laravel-multi-api
为Laravel提供带有代理和多个密钥支持的异步API包装器
Requires
- ext-intl: *
- caseyamcl/guzzle_retry_middleware: ^2.7
- guzzlehttp/guzzle: ^7.4
- illuminate/cache: ^9.0
- illuminate/collections: ^9.0
- illuminate/config: ^9.0
- illuminate/http: ^9.0
- illuminate/support: ^9.0
- kevinrob/guzzle-cache-middleware: ^4.0
Requires (Dev)
- infection/infection: ^0.26.6
- nunomaduro/larastan: ^2.1
- orchestra/testbench: ^7.4
- phpmd/phpmd: ^2.12
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-mockery: ^1.0
- phpstan/phpstan-phpunit: ^1.1
- phpstan/phpstan-webmozart-assert: ^1.1
- phpunit/phpunit: ^9.5
- spatie/invade: ^1.0
- symplify/easy-coding-standard: ^10.2
This package is auto-updated.
Last update: 2024-09-05 03:01:25 UTC
README
Laravel Multi API
支持的API
安装
composer require xbnz/laravel-multi-api
确保您的composer.json文件告诉Laravel自动绑定包服务提供者到项目中
"extra": { "laravel": { "dont-discover": [] } },
配置文件
- 发布配置文件
php artisan vendor:publish --tag=ip-resolver php artisan vendor:publish --tag=resolver
// resolver.php 'use_proxy' => (bool), 'proxies' => (array<string>), // https://13.44.34.34:8080, https://user:pass@33.22.55.66:8080 'timeout' => (int), // seconds 'cache_period' => (int) //seconds, 'async_concurrent_requests' => (int), 'use_retries' => (bool), 'tries' => (int), 'retry_sleep' => (float), // seconds 'retry_sleep_multiplier' => (float) // seconds,
// ip-resolver.php 'api-keys' => (array<Driver>), /** * IpGeolocationDotIoDriver::class => [env(KEY_1), env(KEY_2), ...], */ /** * Visit https://mtr.sh/probes.json to retrieve the list of probe IDs */ \XbNz\Resolver\Domain\Ip\Drivers\MtrDotShMtrDriver::class => [ 'search' => (array<string>) ], \XbNz\Resolver\Domain\Ip\Drivers\MtrDotShPingDriver::class => [ 'search' => (array<string>) ],
支持多个API密钥
您可以在配置文件中为每个驱动程序配置多个API密钥。API密钥将根据每个HTTP请求随机选择。如果您选择了使用重试功能,密钥将在每次尝试时重新加载。
缓存
默认情况下强制执行缓存,因为替代方案会迅速耗尽您的速率限制。如果您使用的是时间敏感的服务,请使用Laravel的Config外观在API调用之前减少缓存。
Config::set(['resolver.cache_period' => 1]);
代理
支持HTTP,HTTPS,SOCKS。请使用上面指定的URL结构。如果您选择了使用重试功能,代理将在每次尝试时重新加载。
API停机可能会干扰您的应用程序。请提前计划。
地理位置
您可以从每个IP API期望的最小规范化响应。注意可能有null值,请参阅NormalizedGeolocationResultsData::class的结构。
- 查询的IP地址
- 国家
- 城市
- 纬度
- 经度
- 组织/ISP
组合所有API
您可以使用所有API来接收有关IP的完整信息,以创建一个综合报告。如果您有多个IP和驱动程序,请在resolver.php
配置文件中增加异步值以加快处理速度。
public function example(Resolver $resolver) { $result = $resolver ->ip() ->withIps(['8.8.8.8', '2606:4700:4700::1111']) ->ipGeolocationDotIo() ->ipApiDotCom() ->ipInfoDotIo() ->normalize(); // ... }
原始API输出
如果您不希望接收压缩的规范化信息,可以使用原始方法
public function example(Resolver $resolver) { $result = $resolver ->ip() ->withIps(['8.8.8.8', '2606:4700:4700::1111']) ->ipGeolocationDotIo() ->ipApiDotCom() ->ipInfoDotIo() ->raw(); // ... }
替代链式操作
public function example(Resolver $resolver) { $result = $resolver ->ip() ->withIps(['8.8.8.8', '2606:4700:4700::1111']) ->withDrivers([ IpGeolocationDotIoDriver::class, // other drivers... ]) ->normalize(); // ... }
使用MTR.sh API进行停机监控
MTR.sh是一个Looking Glass API,它为您提供了访问全球数百个网络的能力。这对于停机监控非常有用。MTR.sh API的问题在于结果对编程语言不友好。但对于Laravel开发者来说,这种情况已经不再存在。
MTR.sh搜索词示例
选择您想要使用的MTR.sh网络
要获取完整列表,请访问https://mtr.sh/probes.json
public function example(Resolver $resolver) { $result = $resolver ->ip() ->withIps(['1.1.1.1']) ->mtrDotShMtr() ->mtrDotShPing() ->normalize(); // ... }
关于设计的简要说明
如果您想扩展该包以支持其他API,请注意以下事项
-
每个端点一个驱动程序 -- GET: https://someapi.io/geo/1.1.1.1: SomeApiGeoDriver::class -- POST: https://someapi.io/geo/bulk: SomeApiGeoBulkDriver::class
-
AuthStrategies
和RetryStrategies
负责应用API密钥头、路径和查询参数,而不是驱动程序。 -
Normalize()
功能仅在存在Mapper::class
且supports()
目标Driver::class
时才起作用。 -
映射器、驱动程序和策略都注册在
ResolverServiceProvider::class
和IpServiceProvider::class
中 -
新的API类别,如货币转换API驱动程序,将遵循相同的模式:在理论上的
CurrencyServiceProvider::class
中注册
贡献
欢迎拉取请求和问题。