Jump to content

need help with this Geolocation IP script


svgmx5

Recommended Posts

I'm playing and testing this Script i found online that gets your location based on your IP. The location is working pretty good, however i can't seem to get it to find locations nearby, or other cities/towns nearby.

 

I've looked over the docs provided on their website, and i'm using their php class scripts they provide but that part is not working for some reason. I'm wondering if anyone here can take a look at it or better yet if someone has actually used this before.

 

The script is located at

http://www.geoplugin.com/webservices/php

 

And this is the class i'm using

<?php

class geoPlugin {

//the geoPlugin server
var $host = 'http://www.geoplugin.net/php.gp?ip={IP}&base_currency={CURRENCY}';

//the default base currency
var $currency = 'USD';

//initiate the geoPlugin vars
var $ip = null;
var $city = null;
var $region = null;
var $areaCode = null;
var $dmaCode = null;
var $countryCode = null;
var $countryName = null;
var $continentCode = null;
var $latitute = null;
var $longitude = null;
var $currencyCode = null;
var $currencySymbol = null;
var $currencyConverter = null;

function geoPlugin() {

}

function locate($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);

	//set the geoPlugin vars
	$this->ip = $ip;
	$this->city = $data['geoplugin_city'];
	$this->region = $data['geoplugin_region'];
	$this->areaCode = $data['geoplugin_areaCode'];
	$this->dmaCode = $data['geoplugin_dmaCode'];
	$this->countryCode = $data['geoplugin_countryCode'];
	$this->countryName = $data['geoplugin_countryName'];
	$this->continentCode = $data['geoplugin_continentCode'];
	$this->latitude = $data['geoplugin_latitude'];
	$this->longitude = $data['geoplugin_longitude'];
	$this->currencyCode = $data['geoplugin_currencyCode'];
	$this->currencySymbol = $data['geoplugin_currencySymbol'];
	$this->currencyConverter = $data['geoplugin_currencyConverter'];

}

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.
	if ( !is_numeric($this->currencyConverter) || $this->currencyConverter == 0 ) {
		trigger_error('geoPlugin class Notice: currencyConverter has no value.', E_USER_NOTICE);
		return $amount;
	}
	if ( !is_numeric($amount) ) {
		trigger_error ('geoPlugin class Warning: The amount passed to geoPlugin::convert is not numeric.', E_USER_WARNING);
		return $amount;
	}
	if ( $symbol === true ) {
		return $this->currencySymbol . round( ($amount * $this->currencyConverter), $float );
	} else {
		return round( ($amount * $this->currencyConverter), $float );
	}
}

function nearby($radius=100, $limit=null) {

	if ( !is_numeric($this->latitude) || !is_numeric($this->longitude) ) {
		trigger_error ('geoPlugin class Warning: Incorrect latitude or longitude values.', E_USER_NOTICE);
		return array( array() );
	}

	$host = "http://www.geoplugin.net/extras/nearby.gp?lat=" . $this->latitude . "&long=" . $this->longitude . "&radius={$radius}";

	if ( is_numeric($limit) )
		$host .= "&limit={$limit}";

	return unserialize( $this->fetch($host) );

}


}

?>

 

 

and this is the actual script that echos the results


<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
// If we wanted to change the base currency, we would uncomment the following line
// $geoplugin->currency = 'EUR';

$geoplugin->locate();

echo "Geolocation results for {$geoplugin->ip}: <br />\n".
"City: {$geoplugin->city} <br />\n".
"Region: {$geoplugin->region} <br />\n".
"Area Code: {$geoplugin->areaCode} <br />\n".
"DMA Code: {$geoplugin->dmaCode} <br />\n".
"Country Name: {$geoplugin->countryName} <br />\n".
"Country Code: {$geoplugin->countryCode} <br />\n".
"Longitude: {$geoplugin->longitude} <br />\n".
"Latitude: {$geoplugin->latitude} <br />\n".
"Currency Code: {$geoplugin->currencyCode} <br />\n".
"Currency Symbol: {$geoplugin->currencySymbol} <br />\n".
"Exchange Rate: {$geoplugin->currencyConverter} <br />\n";

if ( $geoplugin->currency != $geoplugin->currencyCode ) {
//our visitor is not using the same currency as the base currency
echo "<p>At todays rate, US$100 will cost you " . $geoplugin->convert(100) ." </p>\n";
}

/* find places nearby */
$nearby = $geoplugin->nearby();
if ( isset($nearby[0]['geoplugin_place']) ) {
echo "<pre><p>Some places you may wish to visit near " . $geoplugin->city . ": </p>\n";
foreach ( $nearby as $key => $array ) {

	echo ($key + 1) .":<br />";
	echo "\t Place: " . $array['geoplugin_place'] . "<br />";
	echo "\t Country Code: " . $array['geoplugin_countryCode'] . "<br />";
	echo "\t Region: " . $array['geoplugin_region'] . "<br />";
	echo "\t County: " . $array['geoplugin_county'] . "<br />";
	echo "\t Latitude: " . $array['geoplugin_latitude'] . "<br />";
	echo "\t Longitude: " . $array['geoplugin_longitude'] . "<br />";
	echo "\t Distance (miles): " . $array['geoplugin_distanceMiles'] . "<br />";
	echo "\t Distance (km): " . $array['geoplugin_distanceKilometers'] . "<br />";

}
echo "</pre>\n";
}
?>

 

 

I hope someone can help me, or if you have used something better in the past let me know

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.