PHP code example of scorpsan / yii2-geoip

1. Go to this page and download the library: Download scorpsan/yii2-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/ */

    

scorpsan / yii2-geoip example snippets


...
'components' => [
    ...
    'geoIp' => [
        'class' => 'scorpsan\geoip\GeoIp',
// uncomment next line if you register on sypexgeo.net and paste your key        
//        'keySypex' => 'key-sypexgeo-net-this',
// if need more timeout (default 5 = 5000 millisecond)
//        'timeout' => 6,
    ],
    ...
],
...

var_dump(Yii::$app->geoIp->info);

var_dump(Yii::$app->geoIp->infoDb);

var_dump(Yii::$app->geoIp->getInfo('255.255.255.255'));

var_dump(Yii::$app->geoIp->getInfoDb('255.255.255.255'));

var_dump(Yii::$app->geoIp->ip);

     * Returned information by IP address with following paramters:
     * - `ip`               - Visitor IP address, or IP address specified as parameter.
     * - `city`             - Object City information
     * -- [id] => 625144
     * -- [lat] => 53.9
     * -- [lon] => 27.56667
     * -- [name_ru] => Минск
     * -- [name_en] => Minsk
     * -- [name_de] => Minsk
     * -- [name_fr] => Minsk
     * -- [name_it] => Minsk
     * -- [name_es] => Minsk
     * -- [name_pt] => Minsk
     * -- [okato] => 5000000000
     * -- [vk] => 282
     * -- [population] => 1742124
     * - `region`           - Object Region information
     * -- [id] => 625143
     * -- [lat] => 53.9
     * -- [lon] => 27.57
     * -- [name_ru] => Минск
     * -- [name_en] => Horad Minsk
     * -- [name_de] => Minsk
     * -- [name_fr] => Minsk
     * -- [name_it] => Minsk
     * -- [name_es] => Minsk
     * -- [name_pt] => Minsk
     * -- [iso] => BY-HM
     * -- [timezone] => Europe/Minsk
     * -- [okato] => 5
     * -- [auto] => 7
     * -- [vk] => 0
     * -- [utc] => 3
     * - `country`          - Object Country information
     * -- [id] => 36
     * -- [iso] => BY
     * -- [continent] => EU
     * -- [lat] => 53
     * -- [lon] => 28
     * -- [name_ru] => Беларусь
     * -- [name_en] => Belarus
     * -- [name_de] => Weißrussland
     * -- [name_fr] => Biélorussie
     * -- [name_it] => Bielorussia
     * -- [name_es] => Bielorrusia
     * -- [name_pt] => Bielorrússia
     * -- [timezone] => Europe/Minsk
     * -- [area] => 207600
     * -- [population] => 9685000
     * -- [capital_id] => 625144
     * -- [capital_ru] => Минск
     * -- [capital_en] => Minsk
     * -- [cur_code] => BYR
     * -- [phone] => 375
     * -- [neighbours] => PL,LT,UA,RU,LV
     * -- [vk] => 3
     * -- [utc] => 3
     * - `error`            - Error data.
     * - `request`          - Request code.
     * - `created`          - Date created info in database.
     * - `timestamp`        - Timestamp request.
     *
     * @return array|false
     */

     * Returned information by IP address with following paramters:
     * - `ipAddress`       - Visitor IP address, or IP address specified as parameter.
     * - `countryName`     - Name Country in English.
     * - `countryCode`     - Two-letter ISO 3166-1 alpha-2 country code.
     *
     * @return array|false
     */

/** Get User Location */
        //unset($session['_location']);
        if (isset($session['_location']) && !empty($session['_location']['userCountry'])) {
            $app->params['userCountry'] = $session['_location']['userCountry'];
            $app->params['userCountryCode'] = $session['_location']['userCountryCode'];
            $app->params['userCountryRegion'] = $session['_location']['userCountryRegion'];
            $app->params['userCountryCity'] = $session['_location']['userCountryCity'];
            $app->params['userPhoneCode'] = $session['_location']['userPhoneCode'];
        } else {
            //$geoip = $app->geoip->getInfo('93.176.236.137');
            $geoip = $app->geoip->info;
            if (isset($geoip->ip)) :
                $app->params['userCountry'] = $geoip->country->name_en;
                $app->params['userCountryCode'] = $geoip->country->iso;
                $app->params['userCountryRegion'] = $geoip->region->name_en;
                $app->params['userCountryCity'] = $geoip->city->name_en;
                $app->params['userPhoneCode'] = $geoip->country->phone;
            else :
                $geoip = $app->geoip->infoDb;
                if (isset($geoip['ipAddress'])) :
                    $app->params['userCountry'] = $geoip['countryName'];
                    $app->params['userCountryCode'] = $geoip['countryCode'];
                    $app->params['userCountryRegion'] = $geoip['regionName'];
                    $app->params['userCountryCity'] = $geoip['cityName'];
                    $app->params['userPhoneCode'] = $geoip['iddCode'];
                endif;
            endif;
            $session['_location'] = [
                'userCountry' => $app->params['userCountry'],
                'userCountryCode' => $app->params['userCountryCode'],
                'userCountryRegion' => $app->params['userCountryRegion'],
                'userCountryCity' => $app->params['userCountryCity'],
                'userPhoneCode' => $app->params['userPhoneCode'],
            ];
        }

return [
    ...
    'userCountry' => 'World',
    'userCountryCode' => 'wl',
    'userCountryRegion' => '',
    'userCountryCity' => '',
    'userPhoneCode' => '1',
    ...
];