This extension allows you to easily integrate the geoPlugin webservice into your Yii applications.
To download and/or contribute to the extension, please use the github repository.What is geoPlugin? ¶
geoPlugin offers you, the webmaster, the ability to easily geo-localize your visitor down to the city they are in, know what currency they use and the up-to-date currency exchange rate of their currency versus yours.
Requirements ¶
Developed with Yii 1.1.6
Usage ¶
Unpack the contents of the extension to your protected/extensions directory. Once you do that, the following is an example on how to use it:
Yii::import('ext.EGeoIP');
$geoIp = new EGeoIP();
$geoIp->locate('88.27.28.44'); // use your IP
echo 'Information regarding IP: <b>'.$geoIp->ip.'</b><br/>';
echo 'City: '.$geoIp->city.'<br>';
echo 'Region: '.$geoIp->region.'<br>';
echo 'Area Code: '.$geoIp->areaCode.'<br>';
echo 'DMA: '.$geoIp->dma.'<br>';
echo 'Country Code: '.$geoIp->countryCode.'<br>';
echo 'Country Name: '.$geoIp->countryName.'<br>';
echo 'Continent Code: '.$geoIp->continentCode.'<br>';
echo 'Latitude: '.$geoIp->latitude.'<br>';
echo 'Longitude: '.$geoIp->longitude.'<br>';
echo 'Currency Symbol: '.$geoIp->currencySymbol.'<br>';
echo 'Currency Code: '.$geoIp->currencyCode.'<br>';
echo 'Currency Converter: '.$geoIp->currencyConverter.'<br/>';
echo 'Converting $10.00 to '.$geoIp->currencyCode.': <b>'.$geoIp->currencyConvert(10).'</b><br/>';
Note ¶
Please make use of the forum post to report errors, requests, and suggestions. Let comments on this extension for coding hints.
Resources ¶
Change Log ¶
version 1.0 ¶
- Initial Public Release
locate
I think if the service is down or something go wrong with the curl/file_get_content call locate should return false instead true.
Small fix makes it 100% compliant with Yii best practices.
Nice work converting the class made by geoPlugin into a Yii compatible extension! I really like it so far.
Only thing I would correct is the fact that you use $_SERVER['REMOTE_ADDR'] on line 58 of the code. You should be using Yii::app()->request->userHostAddress, instead, to adhere to Yii best practices. Other than that, kudos!
@csalvato
Yeah, thought about that but that function is just a shortcut to the same function.
// code inside Yii::app()->request->userHostAddress function return isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:'127.0.0.1';
I have been looking around for a good way to get the 'real' IP address of a visitor, just in case it comes behind proxies but I disliked it as it relies on headers who can be easily spoof.
function get_ip_address() { foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) { if (array_key_exists($key, $_SERVER) === true) { foreach (explode(',', $_SERVER[$key]) as $ip) { if (filter_var($ip, FILTER_VALIDATE_IP) !== false) { return $ip; } } } } }
@tonydspaniard
Yes, it is just a shortcut, but the isset() check makes it slightly more robust, in my opinion..and it makes Yii code a little more readable. That said, it really is a matter of preference. I made the change in my extension and am happy with it, but it is really up to the user. Either way, its a well-put-together extension.
That said, I wouldn't worry about finding a user's "real" IP address if they are behind a proxy. Most people behind proxies understand that this will alter where the internet "thinks" they are from, and actually prefer to see that they are not being traced back to their original location.
@csalvato
Good point, I should change it... Nevertheless, I have recently updated the extension to work with adapters (so people are free to add new ones to the library) that work with:
I found Ip2Nation the most accurate one... Will update it as soon as I have a bit of time though
PS: Thanks for sharing your thoughts!
@tonydspaniard
Thanks for the replies, Tony!
I also wanted to mention that I had a bit of a problem with the extension when checking to see if something was set. Because you use Unserialize() without var_export() on the response, the values entered into the map don't turn into "NULL" very well. In fact, I am not quite sure what is being stored in the variables.
For example, if you use an IP that is in the UK, for example, many times it won't produce a city or region, so you want to just display the country.
If there was no city found, when checking isset($geoIp->city), it will still return true. Same goes for empty( $geoIp->city ). To get around this in Yii, I have to use CHtml::encode() first to translate whatever is in there into a null string.
It's not a big deal, just a bit hackish and not intuitive. Thought you might want to know to increase the robustness of the extension. I couldn't figure out a way to change it myself to make it work well with CMap(), but maybe you do, as you are more experienced?
Got an error sunddenly
For some reason I got this error:
unserialize() [function.unserialize]: Error at offset 0 of 172 bytes
Related to the curl access.
I solved by adding a else clause to the class method fetch:
} else { return unserialize($response); }
Now it's working.
If this is NOT the proper way of solving it, please advice.
Cheers
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.