Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
Moved to built-in fetch and caching utilities, greatly reducing the n…
Browse files Browse the repository at this point in the history
…umber of API calls
  • Loading branch information
Perlkonig committed Sep 5, 2016
1 parent c9502c0 commit 8479c78
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 43 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# v0.1.0
# v1.0.1
## 09/04/2016

1. [](#improved)
* Moved to the built-in fetch and cache utilities, saving on API calls

# v1.0.0
## 09/03/2016

1. [](#new)
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,4 @@ plugins.geoplugin.latitude

## Performance

I don't know enough about the Grav lifecycle to know how often this API call actually occurs. If you have lots of API calls happening, or if you have lots of traffic, this plugin may become problematic.

If this is an area of expertise of yours, I welcome feedback and pull requests.
This plugin uses Grav's built-in caching API, so only one external API call should ever be made for a given IP address unless you clear the cache.
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Geoplugin
version: 1.0.0
version: 1.0.1
description: Geo locates your visitor using GeoPlugin.com
icon: globe
author:
Expand Down
55 changes: 18 additions & 37 deletions classes/geoplugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@
*/

/*
The licence text above has been untouched, but the code has been modified to use
built-in Grav utilities.
*/

namespace Grav\Plugin;

use Grav\Common\GPM\Response;

class geoPlugin {

//the geoPlugin server
Expand Down Expand Up @@ -55,22 +62,23 @@ function geoPlugin() {

}

function locate($ip = null) {
function locate($cache, $ip = null) {

global $_SERVER;

if ( is_null( $ip ) ) {
$ip = $_SERVER['REMOTE_ADDR'];
}

$host = str_replace( '{IP}', $ip, $this->host );
$host = str_replace( '{CURRENCY}', $this->currency, $host );

$data = array();

$response = $this->fetch($host);

$data = unserialize($response);

$data = $cache->fetch('geoplugin.'.$ip);
if (! $data) {
$host = str_replace( '{IP}', $ip, $this->host );
$host = str_replace( '{CURRENCY}', $this->currency, $host );
$response = Response::get($host);
$data = array();
$data = unserialize($response);
$cache->save('geoplugin.'.$ip, $data);
}

//set the geoPlugin vars
$this->ip = $ip;
Expand All @@ -89,33 +97,6 @@ function locate($ip = null) {

}

function fetch($host) {

if ( function_exists('curl_init') ) {

//use cURL to fetch data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.0');
$response = curl_exec($ch);
curl_close ($ch);

} else if ( ini_get('allow_url_fopen') ) {

//fall back to fopen()
$response = file_get_contents($host, 'r');

} else {

trigger_error ('geoPlugin class Error: Cannot retrieve data. Either compile PHP with cURL support or enable allow_url_fopen in php.ini ', E_USER_ERROR);
return;

}

return $response;
}

function convert($amount, $float=2, $symbol=true) {

//easily convert amounts to geolocated currency.
Expand Down
2 changes: 1 addition & 1 deletion geoplugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function onPluginsInitialized()

// Enable the main event we are interested in
$geoplugin = new geoPlugin();
$geoplugin->locate();
$geoplugin->locate($this->grav['cache']);
$this->config->set('plugins.geoplugin.city', $geoplugin->city);
$this->config->set('plugins.geoplugin.region', $geoplugin->region);
$this->config->set('plugins.geoplugin.areaCode', $geoplugin->areaCode);
Expand Down

0 comments on commit 8479c78

Please sign in to comment.