1. Go to this page and download the library: Download magepsycho/magento2-geoip library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
magepsycho / magento2-geoip example snippets
use MagePsycho\GeoIp\Api\IpLookupInterface;
class YourConsumer
{
public function __construct(private IpLookupInterface $lookup) {}
public function tellMeAbout(string $ip): void
{
$result = $this->lookup->lookup($ip);
if ($result === null) {
// unresolved, or DB not installed — handle gracefully
return;
}
echo $result->getCountryFlagEmoji() . ' ' . $result->getCountryName();
echo $result->getCity();
echo 'AS' . $result->getAsn() . ' ' . $result->getAsnOrganization();
}
}