Reputation: 19
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();
}
Upvotes: 1
Views: 5587
Reputation: 2823
I'm currently using Laravel 9.
Since you have this displaying location data already, It means you have everything installed and working fine. You only have to make a few changes.
The default behavior of the location service is affected by these lines of code in config\location.php
file:
/*
|--------------------------------------------------------------------------
| 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),
],
The comments before the code gives detailed explanation. Here 'enable' is already set to true.
Now, when you are on a live server and you want info on the current client that accesses your website, simply go to your .env
file in the root folder of your application and add this line of code on a new line at the end of the file:
LOCATION_TESTING=false
To test this in my controller, I simply put this in my LocationController.php
file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Stevebauman\Location\Facades\Location;
class LocationController extends Controller
{
public function store(Request $request)
{
$location = Location::get($request->ip);
dd($location->countryName);
}
}
The output is:
"Nigeria" // app\Http\Controllers\CommentController.php:42
If I dump the $location
variable instead (dd($location
) while accessing my live server from my local PC, I get this:
Stevebauman\Location\Position {#1333 ▼ // app/Http/Controllers/CommentController.php:42
+ip: "177.120.78.226"
+driver: "Stevebauman\Location\Drivers\IpApi"
+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"
}
so you can pick other information such as zipCode, cityName etc. from there as well.
NOTE: If you do this on a local server (localhost) after editing your .env
file, the $location
variable will return a boolean value of false. If you want to return to your testing setup, comment out the line you added to your .env
file or simply change LOCATION_TESTING=true
.
Upvotes: 0
Reputation: 1
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());
}
Upvotes: 0