khaleejinfotech/ip2location

一个用于通过IP地址确定位置(经纬度和代理)的Laravel包

v1 2023-01-30 15:30 UTC

This package is auto-updated.

Last update: 2024-09-29 06:14:51 UTC


README

IP2Location Laravel扩展允许用户通过VPNAPI.io使用IP地址查找国家、地区、城市、坐标、时区和ISP。

注意:此扩展适用于Laravel 8和Laravel 9。

安装

运行命令:composer require khaleejinfotech/ip2location 将包下载到Laravel平台。

安装包后,打开您的Laravel配置文件config/app.php并添加以下行。

在$providers数组中添加此包的服务提供者。

Khaleejinfotech\IP2Location\IP2LocationServiceProvider::class,

使用以下命令发布配置文件:

php artisan vendor:publish --tag="ip2location"

在任何文本编辑器中打开config/ip2location.php并添加从vpnapi.io获得的API密钥

<?php

return [
    'api_key' => env('IP2LOCATION_API')
];

用法

使用以下命令在Laravel中创建一个TestController

php artisan make:controller TestController

在任何文本编辑器中打开app/Http/Controllers/TestController.php。要使用IP2Location,请将以下行添加到控制器文件中。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Khaleejinfotech\IP2Location\IP2Location;

class TestController extends Controller
{
	//Create a lookup function for display
	public function lookup(){

            //Try query the geolocation information of 8.8.8.8 IP address
            $ip2Location = new IP2Location();
            $ip2Location->setIP('8.8.8.8');
            $ip2Location->lookup();
            $response = $ip2Location->getResponse();
		
            echo 'IP Address            : ' . $response->getIp() . "<br>";
		
            echo 'Detect VPN            : ' . $response->isVPN() . "<br>";
            echo 'Detect Proxy          : ' . $response->isProxy() . "<br>";
            echo 'Detect Tor            : ' . $response->isTor() . "<br>";
		
            echo 'Country Code          : ' . $response->getCountryCode() . "<br>";
            echo 'Country Name          : ' . $response->getCountryName() . "<br>";
		
            echo 'Region Name           : ' . $response->getRegionName() . "<br>";
            echo 'Region Code           : ' . $response->getRegionCode() . "<br>";
		
            echo 'City Name             : ' . $response->getCity() . "<br>";
		
            echo 'Latitude              : ' . $response->getLatitude() . "<br>";
            echo 'Longitude             : ' . $response->getLongitude() . "<br>";
		
            echo 'Time Zone             : ' . $response->getTimeZone() . "<br>";
	}
}

将以下行添加到routes/web.php文件中。

Route::get('test', [TestController::class,'lookup');

输入URL localhost:8000/test并运行。您应该看到IP地址8.8.8.8的信息。