Burial0268
Bug Report::
- First, checking for the existence of property is a must. Otherwise you'll encounter errors.
For example (Authentic Experience but not the same ip address):
IP:103.58.137.100 has not got region
property.
{
"longitude": 79.0011,
"timezone": "Asia/Kolkata",
"offset": 19800,
"country": "India",
"ip": "103.58.137.100",
"latitude": 21.9974,
"continent_code": "AS",
"country_code": "IN"
}
- the
break
is in the wrong place and it would cause your multiple API settings to fail.
foreach ($services as $service) {
if ($service->name() === $name) {
$this->serviceSelected = $service;
//where the break should be
}
break; //wrong place
}
Suggestion:
Please consider turning to/adding the option ipinfo.io for much more detailed location and fewer "unknown" hosts, as ipsb marks so many regions as unknown in China.
The difference is that ipinfo.io needs us to sign up, and its free plan allows 50k requests per month(, which in fact is fairly enough in my mind.)
Example code:
<?php
/*
* This file is part of GBCLStudio Project.
*
* Copyright (c) 2023 GBCLStudio PHP Project Team.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace GBCLStudio\GeoIp\Api\Service;
use GBCLStudio\GeoIp\Api\GeoIpInterface;
use GBCLStudio\GeoIp\ServiceResponse;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
class IpInfoIo implements GeoIpInterface
{
private Client $client;
public function __construct()
{
$this->client = new Client([
'base_uri' => 'https://ipinfo.io/',
// 'base_uri' => 'https://ip.sb/',
]);
}
public function name(): string
{
return 'ipinfoio';
}
/** Get IP info from upstream API.
*
* @param string $ip
* @return ServiceResponse
* @throws GuzzleException
*/
// public function get(string $ip): ServiceResponse
// {
// $res = $this->client->get("/geoip/$ip");
// $body = json_decode($res->getBody());
// return (new ServiceResponse())
// ->setCountryCode($body->country_code)
// ->setRegion($body->region)
// ->setIsp($body->isp)
// ->setAddress($ip);
// }
// public function get(string $ip): ServiceResponse
// {
// $res = $this->client->get("/geoip/$ip");
// $body = json_decode($res->getBody());
// // 创建一个新的 ServiceResponse 实例
// $response = new ServiceResponse();
// $response->setAddress($ip);
// // 检查每个属性是否存在,并相应地设置它们
// if (isset($body->country_code))$response->setCountryCode($body->country_code);
// if (isset($body->region))$response->setRegion($body->region);
// if (isset($body->isp))$response->setIsp($body->isp);
// return $response;
// }
public function get(string $ip, string $api_key): ServiceResponse
{
$res = $this->client->get("/$ip/json?token=$api_key");
$body = json_decode($res->getBody());
// 创建一个新的 ServiceResponse 实例
$response = new ServiceResponse();
$response->setAddress($ip);
// 检查每个属性是否存在,并相应地设置它们
if (isset($body->city))
$response->setRegion($body->city);
if (isset($body->region)) {
$response->setCountryCode($body->region);
}
if (isset($body->country))
$response->setIsp($body->country);
return $response;
}
}