PHP code example of chinayin / ip2region

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

    

chinayin / ip2region example snippets


use ip2region\Ip2Region;

$ip = '1.2.3.4';
try {
    $searcher = Ip2Region::newWithFileOnly();
    $region = $searcher->search($ip);
    // 或
    $region = Ip2Region::search($ip);
    var_dump($region);
} catch (\Exception $e) {
    var_dump($e->getMessage());
}

use ip2region\Ip2Region;

$ip = '1.2.3.4';
try {
    $region = Ip2Region::newWithVectorIndex()->search($ip);
    var_dump($region);
} catch (\Exception $e) {
    var_dump($e->getMessage());
}

use ip2region\Ip2Region;

$ip = '1.2.3.4';
try {
    $region = Ip2Region::newWithBuffer()->search($ip);
    var_dump($region);
} catch (\Exception $e) {
    var_dump($e->getMessage());
}