PHP code example of magepsycho / magento2-geoip

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();
    }
}

php bin/magento module:enable MagePsycho_GeoIp --clear-static-content
php bin/magento setup:upgrade

php bin/magento cache:flush

php bin/magento magepsycho:geoip:update
xml
<type name="Your\Module\Model\Whatever">
    <arguments>
        <argument name="lookup" xsi:type="object">MagePsycho\GeoIp\Api\IpLookupInterface</argument>
    </arguments>
</type>