xbnz/laravel-multi-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xbnz/laravel-multi-api

Composer 安装命令:

composer require xbnz/laravel-multi-api

包简介

Asynchronous API wrapper with proxy and multiple key support for Laravel

README 文档

README

GitHub Workflow Status

Laravel Multi API

Supported APIs

Provider Key required Normalized Object
Abstractapi.com NormalizedGeolocationResultsData::class
Abuseipdb.com NormalizedGeolocationResultsData::class
Ipapi.co NormalizedGeolocationResultsData::class
Ipapi.com NormalizedGeolocationResultsData::class
Ip-api.com NormalizedGeolocationResultsData::class
Ipdata.co NormalizedGeolocationResultsData::class
Ipgeolocation.io NormalizedGeolocationResultsData::class
Ipinfo.io NormalizedGeolocationResultsData::class
Mtr.sh: ping MtrDotShPingResultsData::class
Mtr.sh: mtr MtrDotShMtrResultsData::class

Installation

composer require xbnz/laravel-multi-api

Ensure that your composer.json file tells Laravel to auto-wire package service providers to your project:

"extra": {
  "laravel": {
    "dont-discover": []
  }
},

The config files

  • Publish config files:
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>)
],

Support for multiple API keys

You may configure each driver in the config files with multiple API keys. API keys will be chosen randomly per HTTP request. If you have elected to use retry functionality, the key will be rehydrated on every try.

Caching

Caching is enforced by default as the alternative with exhaust your rate limiting very quickly. If you are using a time-sensitive service, use Laravel's Config facade to reduce caching before your API call.

Config::set(['resolver.cache_period' => 1]);

Proxies

HTTP, HTTPS, SOCKS supported. Please use the URL structure denoted above. If you have elected to use retry functionality, the proxy will be rehydrated on every try.

API downtime can disrupt your app. Plan ahead.

Geolocation php (11)

Geolocation

The minimum normalized response you can expect from each IP API. Note there may be null values, refer to NormalizedGeolocationResultsData::class for structure.

  • IP address quried
  • Country
  • City
  • Latitude
  • Longitude
  • Organization/ISP

New Project (1)

Combining all of your APIs

You can receive complete information for an IP using all of your APIs to put together a comprehensive report. Increase your async value in the resolver.php config file to expedite the process if you have many IPs and drivers.

public function example(Resolver $resolver)
{
	$result = $resolver
	    ->ip()
	    ->withIps(['8.8.8.8', '2606:4700:4700::1111'])
	    ->ipGeolocationDotIo()
	    ->ipApiDotCom()
	    ->ipInfoDotIo()
	    ->normalize();
	// ...

}

Raw API output

If you do not wish to receive condensed, normalized information, you may use the raw method:

public function example(Resolver $resolver)
{
	$result = $resolver
        ->ip()
        ->withIps(['8.8.8.8', '2606:4700:4700::1111'])
        ->ipGeolocationDotIo()
        ->ipApiDotCom()
        ->ipInfoDotIo()
        ->raw();
    // ...
}
ℹ️ This works in the same way as normalize(). Keep in mind there is no guarantee of data integrity with this option, the result is returned directly from the API in most cases.

Alternative to chaining

public function example(Resolver $resolver)
{
    $result = $resolver
        ->ip()
        ->withIps(['8.8.8.8', '2606:4700:4700::1111'])
        ->withDrivers([
            IpGeolocationDotIoDriver::class,
            // other drivers...
        ])
        ->normalize();
    // ...
}

Downtime monitoring with the MTR.sh API

MTR.sh is a Looking Glass API that gives you access to hundreds of networks around the world. This is particularly useful for downtime monitoring. The problem with the MTR.sh API is that the result is not friendly for programming languages. This is no longer the case for Laravel developers.

MTR.sh search term examples:

Choose what MTR.sh networks you would like to use

Search type Search term
Country ['Germany', 'Brazil', 'Canada']
City ['Frankfurt', 'Rio', 'Toronto']
Continent ['Europe', 'South America', 'North America']
UN/LOCODE ['defra', 'brrio', 'cator']
ISP ['G-Core Labs', 'Anexia', 'Google']

For a complete list, visit https://mtr.sh/probes.json

public function example(Resolver $resolver)
{
    $result = $resolver
        ->ip()
        ->withIps(['1.1.1.1'])
        ->mtrDotShMtr()
        ->mtrDotShPing()
        ->normalize();
    // ...

}
ℹ️ Some MTR.sh probes may not support IPv6, or may not have some abilities, such as the ability to perform MTR tests. When you specify a search term, if no probes match the IP or test type capability, MtrProbeNotFoundException::class will be thrown

New Project (2)

A quick word on the design

If you would like to extend the package to support other APIs, please keep the following in mind:

  • One driver per endpoint -- GET: https://someapi.io/geo/1.1.1.1: SomeApiGeoDriver::class -- POST: https://someapi.io/geo/bulk: SomeApiGeoBulkDriver::class

  • AuthStrategies & RetryStrategies are responsible for applying api key headers, paths and query params, not the driver.

  • Normalize() functionality will only work if there is a Mapper::class that supports() the target Driver::class

  • Mappers, Drivers, and Strategies are all registered in the ResolverServiceProvider::class & IpServiceProvider::class

  • New API categories like currency conversion API drivers will follow the same pattern: registered in a theoretical CurrencyServiceProvider::class

Contributing

Pull requests and issues are welcome.

License

MIT

xbnz/laravel-multi-api 适用场景与选型建议

xbnz/laravel-multi-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 122 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 xbnz/laravel-multi-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 xbnz/laravel-multi-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 xbnz/laravel-multi-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 122
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-19