geoPlugin Home

PHP Web Service

The PHP web service 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_city' => '',
  'geoplugin_region' => '',
  'geoplugin_areaCode' => '0',
  'geoplugin_dmaCode' => '0',
  'geoplugin_countryCode' => 'US',
  'geoplugin_countryName' => 'United States',
  'geoplugin_continentCode' => 'NA',
  'geoplugin_latitude' => '38',
  'geoplugin_longitude' => '-97',
  'geoplugin_regionCode' => '',
  'geoplugin_regionName' => NULL,
  'geoplugin_currencyCode' => 'USD',
  'geoplugin_currencySymbol' => '$',
  'geoplugin_currencyConverter' => 1,
)


PHP 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".
	"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";
}
?>



This will output:


Geolocation results for 38.107.191.92: 
City:
Region:
Area Code: 0
DMA Code: 0
Country Name: United States
Country Code: US
Longitude: -97
Latitude: 38
Currency Code: USD
Currency Symbol: $
Exchange Rate: 1

Some places you may wish to visit near :

1:
Place: Potwin
Country Code: US
Region: Kansas
County: Butler
Latitude: 37.9719009
Longitude: -97.0006027
Distance (miles): 1.94
Distance (km): 3.12
2:
Place: Whitewater
Country Code: US
Region: Kansas
County: Butler
Latitude: 37.9612007
Longitude: -97.1307983
Distance (miles): 7.61
Distance (km): 12.25
3:
Place: Elbing
Country Code: US
Region: Kansas
County: Butler
Latitude: 38.0545006
Longitude: -97.1284027
Distance (miles): 7.94
Distance (km): 12.78
4:
Place: Burns
Country Code: US
Region: Kansas
County: Marion
Latitude: 38.1222000
Longitude: -96.8634033
Distance (miles): 11.25
Distance (km): 18.1


PHP 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





See also