Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 Bugs Detected and 1 Suggestion #11

Closed
WhXcjm opened this issue Jan 18, 2024 · 3 comments
Closed

2 Bugs Detected and 1 Suggestion #11

WhXcjm opened this issue Jan 18, 2024 · 3 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@WhXcjm
Copy link

WhXcjm commented Jan 18, 2024

Bug Report::

  1. 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"
}
  1. 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;
    }
}
@Burial0268 Burial0268 added the bug Something isn't working label Jan 20, 2024
@Burial0268 Burial0268 self-assigned this Jan 20, 2024
@Burial0268
Copy link
Member

Thanks again for pointing that out. These suggestions will be merged into the next version, after I'm done with my pesky exams for a while!

@Burial0268 Burial0268 added the enhancement New feature or request label Jan 20, 2024
@Burial0268
Copy link
Member

Due to my personal academic reasons and poor time planning, the bugs mentioned under this issue will be fixed in the next six months, and I'm sorry for bringing you a bad experience!

@Burial0268
Copy link
Member

add in 4a2b6ec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants