oselya/ip-geolocation-bundle

Symfony IP geolocation bundle

安装次数: 1,837

依赖者: 0

建议者: 0

安全性: 0

星标: 2

关注者: 2

分支: 0

公开问题: 0

类型:symfony-bundle

0.0.5 2024-07-15 08:16 UTC

This package is auto-updated.

Last update: 2024-09-15 12:54:57 UTC


README

License workflow

安装

composer req oselya/ip-geolocation-bundle

在开始之前,需要进行少量配置

# app/config/ip_geolocation.yaml

ip_geolocation:
  cache_ttl: -1
  maxmind:
    city_path: 'GeoLite2-City.mmdb'
  ip_api_com:
    access_key: 'qwerty'

命令行命令

$ bin/console app:ip:location 92.253.204.162
+----------------+-----------+---------+-----------------+-----------------+
| IP             | Continent | Country | Latitude        | Longitude       |
+----------------+-----------+---------+-----------------+-----------------+
| 92.253.204.162 | EU        | UA      | 48.342449188232 | 24.575370788574 |
+----------------+-----------+---------+-----------------+-----------------+

服务

<?php

declare(strict_types=1);

namespace Oselya\IpGeolocationBundle\Command;

use Oselya\IpGeolocationBundle\GeoIpProvider\GeoIpProviderInterface;
use Oselya\IpGeolocationBundle\ValueObject\Ip;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class IpGeolocationCommand extends Command
{
    public function __construct(private readonly GeoIpProviderInterface $provider)
    {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->setName('app:ip:location')
            ->addArgument('ip', InputArgument::REQUIRED, 'The IP address.')
            ->setDescription('This command allows you to lookup location of IP addresses.');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $location = $this->provider->ipLookup(new Ip($input->getArgument('ip')));

        $table = new Table($output);
        $table
            ->setHeaders(['IP', 'Continent', 'Country', 'Latitude', 'Longitude'])
            ->setRows([
                [
                    $input->getArgument('ip'),
                    $location->getContinent(),
                    $location->getCountry(),
                    $location->getLatitude(),
                    $location->getLongitude(),
                ],
            ]);
        $table->render();

        return Command::SUCCESS;
    }
}