Download the PHP package makidizajnerica/laravel-geolocation without Composer
On this page you can find all versions of the php package makidizajnerica/laravel-geolocation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download makidizajnerica/laravel-geolocation
More information about makidizajnerica/laravel-geolocation
Files in makidizajnerica/laravel-geolocation
Package laravel-geolocation
Short Description GeoLocation package for Laravel.
License MIT
Homepage https://github.com/MakiDizajnerica/laravel-geolocation
Informations about the package laravel-geolocation
Laravel GeoLocation
Laravel package to get the details about region, city, country, timezone, currency, etc. for a given IP address.
Installation
As for registering Service Provider, it is not necessary, Laravel will auto load provider using Package Discovery.
Usage
Using Facade
Return value of the MakiDizajnerica\GeoLocation\Facades\GeoLocation::lookup() is the instance of Illuminate\Support\Collection.
To find out more about Laravel Collections head to Collections - Laravel.
You can also switch driver on runtime using:
Package comes with several predefined drivers,
some of them require API_KEY to work.
You can register those keys inside .env file:
Predefined drivers:
| Driver | URL |
|---|---|
| ipwhois [default] | https://ipwhois.io/ |
| geoplugin | https://www.geoplugin.com/ |
| abstractapi | https://www.abstractapi.com/ |
| ipdata | https://ipdata.co/ |
Depending on the driver that is used it is possible to get different results.
Request Macro
Inside your controller Illuminate\Http\Request $request will have geolocation() method available:
Publishing Config File
To publish geolocation.php config file use command:
Creating Custom Driver
When creating custom driver, be sure to extend MakiDizajnerica\GeoLocation\GeoLocationDriver class.
Then define two methods lookup() and format():
Method lookup($ipAddress) accepts one parametar of type string that represents IP address.
Inside this method you write request logic (sending request, checking response etc.),
after that you just need to return response data as array.
If request is not successful for some reason,
you may throw MakiDizajnerica\GeoLocation\Exceptions\GeoLocationDriverException that will be picked up and reported inside log files.
Method format($data) accepts one parametar of type array that represents your response data returned from lookup() method,
here you can format data as array with your own set of key/value pairs.
Inside CustomDriver class you will have two methods available, options() and apiEndpoint().
Method options($key) accepts one parametar $key of type string and returns driver option based on provided $key.
Method apiEndpoint($ipAddress) accepts one parametar $ipAddress of type string and returns formatted api endpoint url with passed $ipAddress.
If you plan to use Laravel's built in Illuminate\Support\Facades\Http Http Client,
you can use MakiDizajnerica\GeoLocation\Support\HandlingHttpResponse trait inside your CustomDriver class:
Method checkResponse(Response $response) accepts one parameter of type Illuminate\Http\Client\Response,
then returns that response if it was successful,
or otherwise throws MakiDizajnerica\GeoLocation\Exceptions\GeoLocationDriverException.
Method decodeResponseData(Response $response) accepts one parameter of type Illuminate\Http\Client\Response,
then tries to json decode that response and return array containing response data,
or empty array on fail.
Then when you wrote lookup logic and formatted data,
you need to register your driver inside config/geolocation.php config file:
{ip} is placeholder where actual IP address will be placed on apiEndpoint() call.
You can put placeholder either in api_endpoint:
or inside query_params like shown above:
You can also define any other placeholder wraped inside curly brackets like so:
Then on runtime pass array containing that placeholder as key as second parameter to driver() method:
So when you call apiEndpoint() method inside your driver lookup method,
the return value should look something like this:
Author
Nemanja Marijanovic ([email protected])
Licence
Copyright © 2021, Nemanja Marijanovic [email protected]
All rights reserved.
For the full copyright and license information, please view the LICENSE file that was distributed within the source root of this package.