I have Creates custom Analytics Module, Where I can track the Visitor Activity, Here I used the Laravel Package "stevebauman/location" for getting Visitor Location.
The main issue is that, when I statically provide the IP to the Variable, this ip provided me the correct location. on the other hand, When I get IP dynamically. It just provide the IP
$visitor = request()->ip();
How Can I get country Name, Code, Postal Address from the IP in Laravel
$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();
}
您可以创建一个服务,然后可以在您的控制器中使用该服务。
You Can Create Folder With service and but it >>>> I use apiStack
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
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());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.