geoplugin.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /*
  3. This PHP class is free software: you can redistribute it and/or modify
  4. the code under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. However, the license header, copyright and author credits
  8. must not be modified in any form and always be displayed.
  9. This class is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. @author geoPlugin (gp_support@geoplugin.com)
  14. @copyright Copyright geoPlugin (gp_support@geoplugin.com)
  15. $version 1.01
  16. This PHP class uses the PHP Webservice of http://www.geoplugin.com/ to geolocate IP addresses
  17. Geographical location of the IP address (visitor) and locate currency (symbol, code and exchange rate) are returned.
  18. See http://www.geoplugin.com/webservices/php for more specific details of this free service
  19. */
  20. class geoPlugin {
  21. //the geoPlugin server
  22. var $host = 'http://www.geoplugin.net/php.gp?ip={IP}&base_currency={CURRENCY}';
  23. //the default base currency
  24. var $currency = 'USD';
  25. //initiate the geoPlugin vars
  26. var $ip = null;
  27. var $city = null;
  28. var $region = null;
  29. var $areaCode = null;
  30. var $dmaCode = null;
  31. var $countryCode = null;
  32. var $countryName = null;
  33. var $continentCode = null;
  34. var $latitute = null;
  35. var $longitude = null;
  36. var $currencyCode = null;
  37. var $currencySymbol = null;
  38. var $currencyConverter = null;
  39. function geoPlugin() {
  40. }
  41. function locate($ip = null) {
  42. global $_SERVER;
  43. if ( is_null( $ip ) ) {
  44. $ip = $_SERVER['REMOTE_ADDR'];
  45. }
  46. $host = str_replace( '{IP}', $ip, $this->host );
  47. $host = str_replace( '{CURRENCY}', $this->currency, $host );
  48. $data = array();
  49. $response = $this->fetch($host);
  50. $data = unserialize($response);
  51. //set the geoPlugin vars
  52. $this->ip = $ip;
  53. $this->city = $data['geoplugin_city'];
  54. $this->region = $data['geoplugin_region'];
  55. $this->areaCode = $data['geoplugin_areaCode'];
  56. $this->dmaCode = $data['geoplugin_dmaCode'];
  57. $this->countryCode = $data['geoplugin_countryCode'];
  58. $this->countryName = $data['geoplugin_countryName'];
  59. $this->continentCode = $data['geoplugin_continentCode'];
  60. $this->latitude = $data['geoplugin_latitude'];
  61. $this->longitude = $data['geoplugin_longitude'];
  62. $this->currencyCode = $data['geoplugin_currencyCode'];
  63. $this->currencySymbol = $data['geoplugin_currencySymbol'];
  64. $this->currencyConverter = $data['geoplugin_currencyConverter'];
  65. }
  66. function fetch($host) {
  67. if ( function_exists('curl_init') ) {
  68. //use cURL to fetch data
  69. $ch = curl_init();
  70. curl_setopt($ch, CURLOPT_URL, $host);
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  72. curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.0');
  73. $response = curl_exec($ch);
  74. curl_close ($ch);
  75. } else if ( ini_get('allow_url_fopen') ) {
  76. //fall back to fopen()
  77. $response = file_get_contents($host, 'r');
  78. } else {
  79. 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);
  80. return;
  81. }
  82. return $response;
  83. }
  84. function convert($amount, $float=2, $symbol=true) {
  85. //easily convert amounts to geolocated currency.
  86. if ( !is_numeric($this->currencyConverter) || $this->currencyConverter == 0 ) {
  87. trigger_error('geoPlugin class Notice: currencyConverter has no value.', E_USER_NOTICE);
  88. return $amount;
  89. }
  90. if ( !is_numeric($amount) ) {
  91. trigger_error ('geoPlugin class Warning: The amount passed to geoPlugin::convert is not numeric.', E_USER_WARNING);
  92. return $amount;
  93. }
  94. if ( $symbol === true ) {
  95. return $this->currencySymbol . round( ($amount * $this->currencyConverter), $float );
  96. } else {
  97. return round( ($amount * $this->currencyConverter), $float );
  98. }
  99. }
  100. function nearby($radius=10, $limit=null) {
  101. if ( !is_numeric($this->latitude) || !is_numeric($this->longitude) ) {
  102. trigger_error ('geoPlugin class Warning: Incorrect latitude or longitude values.', E_USER_NOTICE);
  103. return array( array() );
  104. }
  105. $host = "http://www.geoplugin.net/extras/nearby.gp?lat=" . $this->latitude . "&long=" . $this->longitude . "&radius={$radius}";
  106. if ( is_numeric($limit) )
  107. $host .= "&limit={$limit}";
  108. return unserialize( $this->fetch($host) );
  109. }
  110. }
  111. ?>