Skip to content

Commit

Permalink
Merge pull request #28 from gmsarates/master
Browse files Browse the repository at this point in the history
Use IP-API.com instead iptolocation
  • Loading branch information
sarfraznawaz2005 committed Mar 26, 2024
2 parents 39eafdc + 5a320c6 commit 0e7d810
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/Config/visitlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@
| and Browser info.
|
| Note: For requests from same IP, it will be cached so no further request is made to http://freegeoip.net
|
| UPDATE: If ip_api is set to "true", it will ignore the iptolocation config.
|
*/

'iptolocation' => true,
// FREE IP API - https://ip-api.com/
'ip_api' => true,

'iptolocation' => false,
'token' => 'PASTE_YOUR_TOKEN', // get your token here: https://ipstack.com/

/*
Expand Down
2 changes: 1 addition & 1 deletion src/Views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

<table cellspacing="0" width="100%" id="table-log"
class="table table-striped table-bordered table-sm table-hover table-condensed dt-responsive nowrap">
@if (config('visitlog.iptolocation'))
@if (config('visitlog.iptolocation') || config('visitlog.ip_api'))
<thead>
<tr>
<th>#</th>
Expand Down
58 changes: 47 additions & 11 deletions src/VisitLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class VisitLog
protected $browser = null;
protected $cachePrefix = 'visitlog';
protected $freegeoipUrl = 'http://api.ipstack.com';
protected $ipApiUrl = 'http://ip-api.com';
protected $tokenString = '&output=json&legacy=1';

/**
Expand Down Expand Up @@ -100,7 +101,8 @@ protected function getData()
{
$ip = $this->getUserIP();
$cacheKey = $this->cachePrefix . $ip;
$url = $this->freegeoipUrl . '/' . $ip . '?' . '&access_key=' . config('visitlog.token') . $this->tokenString;
$url_freegeoip = $this->freegeoipUrl . '/' . $ip . '?' . '&access_key=' . config('visitlog.token') . $this->tokenString;
$url_ipApi = $this->ipApiUrl . '/json/' . $ip;

// basic info
$data = [
Expand All @@ -109,27 +111,61 @@ protected function getData()
'os' => $this->browser->getPlatform() ?: 'Unknown',
];

// info from http://freegeoip.net
if (config('visitlog.iptolocation')) {
// info from https://ip-api.com/
if (config('visitlog.ip_api')) {
if (config('visitlog.cache')) {
$freegeoipData = unserialize(Cache::get($cacheKey));
$ipApiCacheKey = $cacheKey . '_apiip';
$ipApiData = unserialize(Cache::get($ipApiCacheKey));

if (!$freegeoipData) {
$freegeoipData = @json_decode(file_get_contents($url), true);
if (!$ipApiData) {
$ipApiData = @json_decode(file_get_contents($url_ipApi), true);

if ($freegeoipData) {
Cache::forever($cacheKey, serialize($freegeoipData));
if ($ipApiData) {
Cache::forever($ipApiCacheKey, serialize($ipApiData));
}
}
} else {
$freegeoipData = @json_decode(file_get_contents($url), true);
$ipApiData = @json_decode(file_get_contents($url_ipApi), true);
}

if ($freegeoipData) {
$data = array_merge($data, $freegeoipData);
if ($ipApiData && $ipApiData['status'] == 'success') {
$parsedData = [
'country_name' => $ipApiData['country'],
'region_name' => $ipApiData['regionName'],
'city' => $ipApiData['city'],
'zip_code' => $ipApiData['zip'],
'time_zone' => $ipApiData['timezone'],
'latitude' => $ipApiData['lat'],
'longitude' => $ipApiData['lon'],
];

$data = array_merge($data, $parsedData);
}
} else {
// info from http://freegeoip.net
if (config('visitlog.iptolocation')) {
if (config('visitlog.cache')) {
$freegeoipCacheKey = $cacheKey . '_freegeoip';
$freegeoipData = unserialize(Cache::get($freegeoipCacheKey));

if (!$freegeoipData) {
$freegeoipData = @json_decode(file_get_contents($url_freegeoip), true);

if ($freegeoipData) {
Cache::forever($freegeoipCacheKey, serialize($freegeoipData));
}
}
} else {
$freegeoipData = @json_decode(file_get_contents($url_freegeoip), true);
}

if ($freegeoipData) {
$data = array_merge($data, $freegeoipData);
}
}
}


$userData = $this->getUser();

if ($userData) {
Expand Down

0 comments on commit 0e7d810

Please sign in to comment.