evinkuraga/geolocation

一个用于集成IP Info DB服务的Laravel包

v2.0.1 2020-10-27 20:40 UTC

This package is auto-updated.

Last update: 2024-09-06 01:15:28 UTC


README

Laravel的IP Info DB集成

注意

这个包是从evinkuraga/geolocation分支出来的,这样我才能编辑composer文件以接受guzzle ^7.0,这是与Laravel 6.0的后续版本一起工作的必要条件。我已经注意到Guzzle的使用很少,所以我最终会调整它以使用Guzzle 7,或者直接添加到composer中看看会发生什么。

安装

此包需要PHP 5.6+,并包含Laravel 5服务提供者和外观。

要通过composer安装,请将包包含在您的composer.json中。

"evinkuraga/geolocation": "^2.0"

运行composer installcomposer update以下载依赖项,或者您可以运行composer require evinkuraga/geolocation

刷新自动加载器

此时,一些用户可能需要运行命令composer dump-autoload。或者,您可以运行php artisan optimize,它应该包含dump-autoload命令。

Laravel 5集成

要使用此包与Laravel 5一起使用,首先将GeoLocation服务提供者添加到app/config/app.php中的服务提供者列表中。

'providers' => [

  Evinkuraga\GeoLocation\GeoLocationServiceProvider::class
          
];

GeoLocation外观添加到您的别名字符数组中。

'aliases' => [

  'GeoLocation' => Evinkuraga\GeoLocation\Facades\GeoLocation::class,
  
];

使用php artisan vendor:publish --provider="Evinkuraga\GeoLocation\GeoLocationServiceProvider"发布配置和迁移文件。

配置文件

发布配置文件后,您将在config文件夹中找到一个geolocation.php文件。您应该查看这些设置,并在必要时进行更新。

环境变量

您需要将以下内容添加到您的.env文件中,并使用自己的设置更新这些内容

GEOLOCATION_API_KEY=<key>
GEOLOCATION_CACHE=<duration_in_minutes>

获取您的GeoLocation API密钥

在使用此包之前,您必须从IP Info DB获取一个API密钥。请访问http://ipinfodb.com/register.php进行注册并确认您的电子邮件地址后,您的API密钥将显示出来。请复制并设置到您的.env文件中的GEOLOCATION_API_KEY选项。

示例用法

use Evinkuraga\GeoLocation\Contracts\Services\GeoLocation;
use Illuminate\Http\Request;

public function index(GeoLocation $geo, Request $request) 
{
    $ipLocation = $geo->getCity($request->ip());
    
    // if you do $geo->get($request->ip()), the default precision is now city

    // $ipLocation is an IpLocation Object
    
    echo $ipLocation->ipAddress; // e.g. 127.0.0.1
    
    echo $ipLocation->getAddressString(); // e.g. London, United Kingdom
    
    // the object has a toJson() and toArray() method on it 
    // so you can die and dump an array.
    dd($ipLocation->toArray()); 

}

IpLocation的方法

$ipLocation->getStatusCode(); // returns status code of request (e.g. 200)
$ipLocation->getStatusMessage(); // returns any status message to go with code
$ipLocation->getIpAddress(); // the geolocation IP requested
$ipLocation->getCountryCode(); // country code of the IP e.g. GB
$ipLocation->getCountryName(); // country name of the IP e.g. United Kingdom
$ipLocation->getRegionName(); // region name of the IP e.g. England
$ipLocation->getCityName(); // city name of the IP e.g. London
$ipLocation->getZipCode(); // postcode of the IP e.g. SE01 1AA
$ipLocation->getPostCode(); // postcode of the IP e.g. SE01 1AA
$ipLocation->getLatitude(); // latitude of the IP e.g. 53.4030
$ipLocation->getLongitude(); // longitude of the IP e.g. -1.201
$ipLocation->getTimeZone(); // timezone of the IP e.g.
$ipLocation->getAddressString(); // gets the city, region and country as a string
$ipLocation->toArray(); // returns object as an array
$ipLocation->toJson(); // returns object as a json object