Advertisement
Guest User

gro

a guest
Jan 31st, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.51 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class geoPlugin {
  5.    
  6.     //the geoPlugin server
  7.     var $host = 'http://www.geoplugin.net/php.gp?ip={IP}&base_currency={CURRENCY}';
  8.        
  9.     //the default base currency
  10.     var $currency = 'USD';
  11.    
  12.     //initiate the geoPlugin vars
  13.     var $ip = null;
  14.     var $city = null;
  15.     var $region = null;
  16.     var $areaCode = null;
  17.     var $dmaCode = null;
  18.     var $countryCode = null;
  19.     var $countryName = null;
  20.     var $continentCode = null;
  21.     var $latitude = null;
  22.     var $longitude = null;
  23.     var $currencyCode = null;
  24.     var $currencySymbol = null;
  25.     var $currencyConverter = null;
  26.    
  27.     function geoPlugin() {
  28.  
  29.     }
  30.    
  31.     function locate($ip = null) {
  32.        
  33.         global $_SERVER;
  34.        
  35.         if ( is_null( $ip ) ) {
  36.             $ip = $_SERVER['REMOTE_ADDR'];
  37.         }
  38.        
  39.         $host = str_replace( '{IP}', $ip, $this->host );
  40.         $host = str_replace( '{CURRENCY}', $this->currency, $host );
  41.        
  42.         $data = array();
  43.        
  44.         $response = $this->fetch($host);
  45.        
  46.         $data = unserialize($response);
  47.        
  48.         //set the geoPlugin vars
  49.         $this->ip = $ip;
  50.         $this->city = $data['geoplugin_city'];
  51.         $this->region = $data['geoplugin_region'];
  52.         $this->areaCode = $data['geoplugin_areaCode'];
  53.         $this->dmaCode = $data['geoplugin_dmaCode'];
  54.         $this->countryCode = $data['geoplugin_countryCode'];
  55.         $this->countryName = $data['geoplugin_countryName'];
  56.         $this->continentCode = $data['geoplugin_continentCode'];
  57.         $this->latitude = $data['geoplugin_latitude'];
  58.         $this->longitude = $data['geoplugin_longitude'];
  59.         $this->currencyCode = $data['geoplugin_currencyCode'];
  60.         $this->currencySymbol = $data['geoplugin_currencySymbol'];
  61.         $this->currencyConverter = $data['geoplugin_currencyConverter'];
  62.        
  63.     }
  64.    
  65.     function fetch($host) {
  66.  
  67.         if ( function_exists('curl_init') ) {
  68.                        
  69.             //use cURL to fetch data
  70.             $ch = curl_init();
  71.             curl_setopt($ch, CURLOPT_URL, $host);
  72.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  73.             curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.0');
  74.             $response = curl_exec($ch);
  75.             curl_close ($ch);
  76.            
  77.         } else if ( ini_get('allow_url_fopen') ) {
  78.            
  79.             //fall back to fopen()
  80.             $response = file_get_contents($host, 'r');
  81.            
  82.         } else {
  83.  
  84.             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);
  85.             return;
  86.        
  87.         }
  88.        
  89.         return $response;
  90.     }
  91.    
  92.     function convert($amount, $float=2, $symbol=true) {
  93.        
  94.         //easily convert amounts to geolocated currency.
  95.         if ( !is_numeric($this->currencyConverter) || $this->currencyConverter == 0 ) {
  96.             trigger_error('geoPlugin class Notice: currencyConverter has no value.', E_USER_NOTICE);
  97.             return $amount;
  98.         }
  99.         if ( !is_numeric($amount) ) {
  100.             trigger_error ('geoPlugin class Warning: The amount passed to geoPlugin::convert is not numeric.', E_USER_WARNING);
  101.             return $amount;
  102.         }
  103.         if ( $symbol === true ) {
  104.             return $this->currencySymbol . round( ($amount * $this->currencyConverter), $float );
  105.         } else {
  106.             return round( ($amount * $this->currencyConverter), $float );
  107.         }
  108.     }
  109.    
  110.     function nearby($radius=10, $limit=null) {
  111.  
  112.         if ( !is_numeric($this->latitude) || !is_numeric($this->longitude) ) {
  113.             trigger_error ('geoPlugin class Warning: Incorrect latitude or longitude values.', E_USER_NOTICE);
  114.             return array( array() );
  115.         }
  116.        
  117.         $host = "http://www.geoplugin.net/extras/nearby.gp?lat=" . $this->latitude . "&long=" . $this->longitude . "&radius={$radius}";
  118.        
  119.         if ( is_numeric($limit) )
  120.             $host .= "&limit={$limit}";
  121.            
  122.         return unserialize( $this->fetch($host) );
  123.  
  124.     }
  125.  
  126.    
  127. }
  128.  
  129. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement