hamidreza2005/laravel-ip

通过IP地址获取客户端位置的软件包

v2.2.0 2020-09-27 16:56 UTC

This package is auto-updated.

Last update: 2024-09-29 05:49:36 UTC


README

通过IP地址查找位置的软件包 🚀

📥 安装

您可以通过Composer安装此软件包

composer require hamidreza2005/laravel-ip  

安装后,使用以下命令发布配置

php artisan vendor:publish --tag=laravel-ip  

⚙️ 配置

要配置此软件包,请转到 config/ip.php 并选择您的驱动程序

<?php    
    
return [    
   /*    
   |--------------------------------------------------------------------------    
   | Ip Driver    
   |--------------------------------------------------------------------------    
   |    
   | Here is where you can choose your driver for getting location from ip    
   | Supported :    
   |   "ipinfo" => visit https://www.ipinfo.io,    
   |   "ipapi" => visit https://www.ipapi.com,    
   |   "geojs" => visit https://www.geojs.io/    
   | Suggested : "geojs"    
   |    
   */  
    "ip_driver" => "geojs",    
   ...  
  ];  

并放置您的驱动程序的API令牌

<?php     
return [   
   ...  
   "drivers" =>[    
     "ipinfo" =>[    
        "api_token" => 'YOUR_API_KEY'    
     ],    
     "ipapi" =>[    
        "api_token" => "YOUR_API_KEY"    
     ]    
 ]];  

注意:如果您选择 "geojs" 驱动程序,则不需要API令牌。

🔧 使用

use hamidreza2005\laravelIp\Facades\Ip;  
  
Route::get('/', function () {    
  return IP::countryCode();    
});  

将返回以下字符串

DE  

您可以使用其他方法

IP::countryCode(); // return country Code e.g DE  
IP::all(); // return all Details about client's ip  
IP::coordinates(); // return client's coordinates  
IP::ip(); // return all client's ip  
IP::country(); // return all client's country full name e.g Germany
// Or you can use helper function
ip()->country();
ip("coountry"); // Both of them are true  

注意:因为每个驱动程序的结构不同,您应该使用所有方法来访问IP的详细信息

从自定义IP获取位置

如果您想从自定义IP获取位置,您可以使用$ip参数

IP::all("8.8.8.8")

上面的命令从IP 8.8.8.8 获取位置,并得到以下结果

{
  "organization_name": "GOOGLE",
  "accuracy": 1000,
  "asn": 15169,
  "organization": "AS15169 GOOGLE",
  "timezone": "America/Chicago",
  "longitude": "-97.822",
  "country_code3": "USA",
  "area_code": "0",
  "ip": "8.8.8.8",
  "country": "United States",
  "continent_code": "NA",
  "country_code": "US",
  "latitude": "37.751"
}

在ipinfo驱动程序中获取IP的全名

如您所知,ipinfo结构中没有国家全名。因此,如果您想使用ipinfo驱动程序并需要国家全名(例如,法国),您可以在您喜欢的位置创建一个json文件,并在config/ip.php中写入此代码

<?php
return [
	...
	"drivers" =>[  
        "ipinfo" =>[  
            "api_token" => "YOUR_API_TOKEN",  
            "full_country_name_path" => storage_path('default.json')  
		  ]
	 ],  
	 ...
];

例如,default.json文件必须如下所示

{   
  "US": "United State",  
  "DE": "Germany"
}

现在,您可以通过ipinfo驱动程序中的IP::country()获取国家全名

🚫 通过IP阻止客户端

如果您想通过IP或其他方式阻止客户端,您必须在app/Http/kernel.php中添加此中间件

protected $middleware = [    
   ...  
   \hamidreza2005\laravelIp\Middleware\laravelIp::class,  
   ...  
];  

并在config/ip.php中选择黑名单或白名单中的IP或国家

<?php  
return [  
   ...  
   "blocking"=>[    
        /*    
       * The values in this array won't access to website   
       */   
        "blacklist"=>[    
           "countryCode"=>["NK"],    
           "ip"=>["5.61.44.90"],  
           "coordinates" =>[[1.2.3.4,45.5.8.9]]    
       ],    
       /*    
       * only The values in this array can access to website   
       */  
       "whitelist"=>[    
//                  "countryCode"=>["US"]    
        ]    
    ]  
   ...  
];  

📜 许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。

🙋 贡献

如果您发现问题,或有更好的实现方式,请随时提交问题或拉取请求。