geoPlugin Home
Command disabled: index

PHP Geolocation Web Service

The PHP Geolocation web service API 1) allows you to directly program your back-end PHP scripts to deliver dynamic geo-localized pages using the PHP array provided by geoPlugin.
To access this service, add the following url to a remote include call

	http://www.geoplugin.net/php.gp?ip=xx.xx.xx.xx


Of course, substitute the xx's with your visitor's IP number.

As an example, the following PHP snippet

  echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])));


Will output:

array (
  'geoplugin_request' => '172.69.214.65',
  'geoplugin_status' => 200,
  'geoplugin_delay' => '1ms',
  'geoplugin_credit' => 'Some of the returned data includes GeoLite2 data created by MaxMind, available from https://www.maxmind.com.',
  'geoplugin_city' => 'Toronto',
  'geoplugin_region' => 'Ontario',
  'geoplugin_regionCode' => 'ON',
  'geoplugin_regionName' => 'Ontario',
  'geoplugin_areaCode' => '',
  'geoplugin_dmaCode' => '',
  'geoplugin_countryCode' => 'CA',
  'geoplugin_countryName' => 'Canada',
  'geoplugin_inEU' => 0,
  'geoplugin_euVATrate' => false,
  'geoplugin_continentCode' => 'NA',
  'geoplugin_continentName' => 'North America',
  'geoplugin_latitude' => '43.6547',
  'geoplugin_longitude' => '-79.3623',
  'geoplugin_locationAccuracyRadius' => '20',
  'geoplugin_timezone' => 'America/Toronto',
  'geoplugin_currencyCode' => 'CAD',
  'geoplugin_currencySymbol' => NULL,
  'geoplugin_currencySymbol_UTF8' => '',
  'geoplugin_currencyConverter' => '1.4313',
)


PHP Geolocation Class

You can download the geoPlugin class to simplify using the PHP Web service.

View Class Source

A detailed example of how to use the class (with comments) is included in the class package.
An abbreviated version of the example is shown below

<?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".
	"Region Code: {$geoplugin->regionCode} <br />\n".
	"Region Name: {$geoplugin->regionName} <br />\n".
	"DMA Code: {$geoplugin->dmaCode} <br />\n".
	"Country Name: {$geoplugin->countryName} <br />\n".
	"Country Code: {$geoplugin->countryCode} <br />\n".
	"In the EU?: {$geoplugin->inEU} <br />\n".
	"EU VAT Rate: {$geoplugin->euVATrate} <br />\n".
	"Latitude: {$geoplugin->latitude} <br />\n".
	"Longitude: {$geoplugin->longitude} <br />\n".
	"Radius of Accuracy (Miles): {$geoplugin->locationAccuracyRadius} <br />\n".
	"Timezone: {$geoplugin->timezone}  <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";
}
?>



This will output:


Geolocation results for 172.69.214.65: 
City: Toronto
Region: Ontario
Region Code: ON
Region Name: Ontario
DMA Code:
Country Name: Canada
Country Code: CA
In the EU?: 0
EU VAT Rate:
Latitude: 43.6547
Longitude: -79.3623
Radius of Accuracy (Miles): 20
Timezone: America/Toronto
Currency Code: CAD
Currency Symbol:
Exchange Rate: 1.4313

At todays rate, US$100 will cost you 143.13

Some places you may wish to visit near Toronto:

1:
Place: Old Toronto
Country Code: CA
Region: Ontario
County:
Latitude: 43.6499900
Longitude: -79.3820600
Distance (miles): 1.04
Distance (km): 1.67
2:
Place: Grange Park
Country Code: CA
Region: Ontario
County:
Latitude: 43.6526100
Longitude: -79.3924900
Distance (miles): 1.52
Distance (km): 2.44
3:
Place: Kensington Market
Country Code: CA
Region: Ontario
County:
Latitude: 43.6550800
Longitude: -79.4027000
Distance (miles): 2.02
Distance (km): 3.25
4:
Place: East York
Country Code: CA
Region: Ontario
County:
Latitude: 43.6905300
Longitude: -79.3279400
Distance (miles): 3.01
Distance (km): 4.85
5:
Place: Davisville
Country Code: CA
Region: Ontario
County:
Latitude: 43.7019400
Longitude: -79.3888900
Distance (miles): 3.52
Distance (km): 5.67


PHP Geolocation Currency Converter

The “geoplugin_currencyConverter” array key is the conversion rate for the currency converter base currency.
Like all calls to any of geoPlugin's web services, the default base_currency is USD ($US).
Thus, if your base currency is NOT $US, then you must add the variable base_currency=XXX to the call to geoplugin.net
eg

http://www.geoplugin.net/php.gp?base_currency=EUR

Now geoplugin_currencyConverter will output the exchange rate of one Euro for your visitor.

The base_currency value must be a valid ISO 4217 3-letter code.




An example for using the currency converter in PHP is given below.

 
<?php
 
function cc($amount) {
	global $geoPlugin_array;
	if ( isset($geoPlugin_array['geoplugin_currencyCode']) && $geoPlugin_array['geoplugin_currencyCode'] != 'USD' ) { 
		return '(' . $geoPlugin_array['geoplugin_currencySymbol'] . round( ($amount * $geoPlugin_array['geoplugin_currencyConverter']),2) . ')';
	} 
	return false;
}
 
$geoPlugin_array = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
 
echo '<h3>Product A costs $800 ' . cc(800) . '</h3>';
 
?>



Product A costs US$800 (1145.04)





See also

1) This product includes GeoLite data created by MaxMind, available from http://maxmind.com/
 
 


Acceptable Use Policy    Privacy Policy and User Agreement    Contact geoPlugin


©2006 - 2025 geoPlugin® is a registered trademark of GEOPLUGIN, SAS