如何在Laravel从ip获取国家



我创建了自定义分析模块,在那里我可以跟踪访问者的活动,在这里我使用了Laravel包"stevebauman/location"获取访问者位置。

主要问题是,当我静态地向变量提供IP时,这个IP为我提供了正确的位置。另一方面,当我动态获取IP时。它只提供IP

$visitor = request()->ip();

如何从Laravel的IP中获取国家名称,代码,邮政地址

$visitor = request()->ip();
$traffic = PageVisits::where('ip_address', $visitor)->where('property_id', $id)
->whereMonth('created_at', Carbon::now()->month)->first();
if(auth()->check() && auth()->user()->usertype == 'Admin'){

}else{
if (!$traffic) {
$traffic = new PageVisits();
$traffic->ip_address = $visitor;
$traffic->property_id = $id;
$position = Location::get('https://'.$visitor);
$traffic->country = $position->countryName;
$traffic->agency_id = $property->agency_id ?? '';
$traffic->save();
}

您可以使用service和but it创建文件夹>>>>我使用apiStack

<?php
namespace AppServices;
use IlluminateSupportFacadesHttp;
class IpStack
{
protected $key;
protected $baseUrl = 'http://api.ipstack.com/';
public function __construct($key)
{
$this->key = $key;
}
public function get($ip)
{
// http://api.ipstack.com/(ip)?access_key=(key)
$response = Http::baseUrl($this->baseUrl)
->get($ip, [
'access_key' => $this->key,
]);
return $response->json();
}
}

for call in controller

{
$geoip = new IpStack(config('services.ipstack.key'));
$response = $geoip->get(request()->ip());
}

我目前使用的是Laravel 9。

既然你已经有了这个显示位置数据,这意味着你已经安装好了所有的东西并且工作正常。你只需要做一些改动。

位置服务的默认行为受到configlocation.php文件中以下几行代码的影响:


/*
|--------------------------------------------------------------------------
| Localhost Testing
|--------------------------------------------------------------------------
|
| If your running your website locally and want to test different
| IP addresses to see location detection, set 'enabled' to true.
|
| The testing IP address is a Google host in the United-States.
|
*/
'testing' => [
'ip' => '66.102.0.0',
'enabled' => env('LOCATION_TESTING', true),
],

代码前的注释给出了详细的解释。这里的"enable"已经被设置为true。

现在,当你在一个实时服务器上,你想要关于当前客户端访问你的网站的信息,简单地去你的.env文件在你的应用程序的根文件夹,并在文件的末尾添加这行代码:

LOCATION_TESTING=false
为了在我的控制器中测试这一点,我简单地把它放在我的LocationController.php文件中:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use StevebaumanLocationFacadesLocation;
class LocationController extends Controller
{
public function store(Request $request)
{
$location = Location::get($request->ip);
dd($location->countryName);
}
}

输出为:

"Nigeria" // appHttpControllersCommentController.php:42

如果我转储$location变量(dd($location),而从我的本地PC访问我的实时服务器,我得到这个:

StevebaumanLocationPosition {#1333 ▼ // app/Http/Controllers/CommentController.php:42
+ip: "177.120.78.226"
+driver: "StevebaumanLocationDriversIpApi"
+countryName: "Nigeria"
+countryCode: "NG"
+regionCode: "RI"
+regionName: "Rivers State"
+cityName: "Port Harcourt"
+zipCode: ""
+isoCode: null
+postalCode: null
+latitude: "4.7731"
+longitude: "7.0085"
+metroCode: null
+areaCode: "RI"
+timezone: "Africa/Lagos"
}

所以你可以从那里选择其他信息,如zipCode, cityName等。

注意:如果在编辑.env文件后在本地服务器(localhost)上执行此操作,则$location变量将返回一个布尔值false。如果您想回到您的测试设置,注释掉您添加到.env文件中的行,或者简单地更改LOCATION_TESTING=true

最新更新