PHP code example of makidizajnerica / laravel-geolocation
1. Go to this page and download the library: Download makidizajnerica/laravel-geolocation library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
makidizajnerica / laravel-geolocation example snippets
use MakiDizajnerica\GeoLocation\Facades\GeoLocation;
$collection = GeoLocation::lookup('8.8.4.4');
echo $collection->get('success');
// true
echo $collection->get('ip');
// 8.8.4.4
echo $collection->get('continent');
// North America
echo $collection->get('continentCode');
// NA
echo $collection->get('country');
// United States
echo $collection->get('countryCode');
// US
echo $collection->get('countryFlag');
// https://cdn.ipwhois.io/flags/us.svg
echo $collection->get('countryCapital');
// Washington
echo $collection->get('region');
// California
echo $collection->get('city');
// Mountain View
echo $collection->get('timezone');
// America/Los_Angeles
echo $collection->get('timezoneName');
// Pacific Standard Time
echo $collection->get('currency');
// US Dollar
echo $collection->get('currencyCode');
// USD
echo $collection->get('currencySymbol');
// $
echo $collection->get('latitude');
// 37.3860517
echo $collection->get('longitude');
// -122.0838511
var_dump($collection->toArray());
// Array
// (
// [success] => 1
// [ip] => 8.8.4.4
// [continent] => North America
// [continentCode] => NA
// [country] => United States
// [countryCode] => US
// [countryFlag] => https://cdn.ipwhois.io/flags/us.svg
// [countryCapital] => Washington
// [region] => California
// [city] => Mountain View
// [timezone] => America/Los_Angeles
// [timezoneName] => Pacific Standard Time
// [currency] => US Dollar
// [currencyCode] => USD
// [currencySymbol] => $
// [latitude] => 37.3860517
// [longitude] => -122.0838511
// )
use MakiDizajnerica\GeoLocation\Facades\GeoLocation;
$collection = GeoLocation::driver('geoplugin')->lookup('8.8.4.4');
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::get('/', function (Request $request) {
dd($request->geolocation()->toArray());
return view('welcome');
});
// Array
// (
// [success] => 1
// [ip] => 8.8.4.4
// [continent] => North America
// [continentCode] => NA
// [country] => United States
// [countryCode] => US
// [countryFlag] => https://cdn.ipwhois.io/flags/us.svg
// [countryCapital] => Washington
// [region] => California
// [city] => Mountain View
// [timezone] => America/Los_Angeles
// [timezoneName] => Pacific Standard Time
// [currency] => US Dollar
// [currencyCode] => USD
// [currencySymbol] => $
// [latitude] => 37.3860517
// [longitude] => -122.0838511
// )
use MakiDizajnerica\GeoLocation\GeoLocationDriver;
class CustomDriver extends GeoLocationDriver
{
public function lookup($ipAddress): array
{
//
}
public function format(array $data): array
{
return [
//
];
}
}
use Illuminate\Support\Facades\Http;
use MakiDizajnerica\GeoLocation\GeoLocationDriver;
use MakiDizajnerica\GeoLocation\Support\HandlingHttpResponse;
class CustomDriver extends GeoLocationDriver
{
use HandlingHttpResponse;
public function lookup($ipAddress): array
{
$response = Http::get($this->apiEndpoint($ipAddress));
$data = $this->decodeResponseData(
$this->checkResponse($response)
);
return $data;
}
public function format(array $data): array
{
return [
'ip' => $data['ip'],
'continent' => $data['continent'],
'country' => $data['country'],
'region' => $data['region'],
'city' => $data['city'],
'timezone' => $data['timezone'],
'currency' => $data['currency'],
];
}
}