PHP code example of larryli / ipv4-yii2

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

    

larryli / ipv4-yii2 example snippets


Yii::setAlias('@ipv4-yii2', dirname(__DIR__) . '/vendor/larryli/ipv4-yii2');

// ipv4 component
'ipv4' => [
    'class' => '\larryli\ipv4\yii2\IPv4',
    // 'db' => 'db',
    // 'database' => '\larryli\ipv4\yii2\Database',
    // 'prefix' => 'ipv4_',
    'providers' => [
        'monipdb' => [
            // 'class' => '\larryli\ipv4\MonipdbQuery',
            'filename' => '@runtime/17monipdb.dat',
        ],
        'qqwry' => [
            // 'class' => '\larryli\ipv4\QqwryQuery',
            'filename' => '@runtime/qqwry.dat',
        ],
        'full' => [
            // 'class' => '\larryli\ipv4\FullQuery',
            'providers' => ['monipdb', 'qqwry'], // ex. 'monipdb', 'qqwry', ['qqwry', 'monipdb']
        ],
        'mini' => [
            // 'class' => '\larryli\ipv4\MiniQuery',
            'providers' => 'full',   // ex. ['monipdb', 'qqwry'], 'monipdb', 'qqwry', ['qqwry', 'monipdb']
        ],
        'china' => [
            // 'class' => '\larryli\ipv4\MiniQuery',
            'providers' => 'full',
        ],
        'world' => [
            // 'class' => '\larryli\ipv4\MiniQuery',
            'providers' => 'full',
        ],
        'freeipip' => [
            // 'class' => '\larryli\ipv4\FreeipipQuery',
        ],
        // 'taobao' => [
            // 'class' => '\larryli\ipv4\TaobaoQuery',
        // ],
        // 'sina' => [
            // 'class' => '\larryli\ipv4\SinaQuery',
        // ],
        // 'BaiduMap' => [
            // 'class' => '\larryli\ipv4\BaidumapQuery',
        // ],
    ],
],

// ipv4 command
'ipv4' => [
    'class' => 'larryli\ipv4\yii2\commands\Ipv4Controller',
],

use Yii;
Yii::$app->get('ipv4')->getQuery('full')->find(ip2long('127.0.0.1'));
Yii::$app->get('ipv4')->__get('full')->find(ip2long('127.0.0.1'));
Yii::$app->ipv4->full->find(ip2long('127.0.0.1'));

foreach (Yii::$app->ipv4->getQueries() as $query) {
    $query->find(ip2long('127.0.0.1'));
}

namespace app\models;

use larryli\ipv4\yii2\models\Division as BaseDivision;

/**
 * Class Division
 * @package app\models
 */
class Division extends BaseDivision
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return "{{%ipv4_divisions}}";
    }
}

namespace app\models;

use larryli\ipv4\yii2\models\Index;

/**
 * Class Full
 * @package app\models
 *
 * @property string $ip
 * @property Division $division
 */
class Full extends Index
{
    /**
     * @return string
     */
    static public function divisionClassName()
    {
        return Division::className();
    }
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%ipv4_full}}';
    }
}

$model = Full::findOneByIp(ip2long('127.0.0.1'));
if (!empty($model) && !empty(!$model->division)) {
    echo $model->division->name;
}
shell
cp vendor/larryli/ipv4-yii2/migrations/*.php migrations/
shell
./yii ipv4/init
shell
./yii ipv4/benchmark        # 性能测试
./yii ipv4/clean            # 清除全部数据
./yii ipv4/clean file       # 清除下载的文件数据
./yii ipv4/clean database   # 清除生成的数据库数据
./yii ipv4/dump             # 导出原始数据
./yii ipv4/dump division    # 导出排序好的全部地址列表
./yii ipv4/dump division_id # 导出排序好的全部地址和猜测行政区域代码列表
dump
memory_limit