PHP code example of larva / laravel-geoip2

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

    

larva / laravel-geoip2 example snippets


return [
    'enable' => true,
    
    'default'  => '--',      // 缺省展示值
    'language' => ['zh-CN'], // 语言

    'db-country' => null,    // 自定义的country库绝对地址
    'db-city'    => null,    // 自定义的city库绝对地址
    'db-asn'     => null,    // 自定义的asn库绝对地址
];

use Larva\GeoIp2\GeoIp2;
use Larva\GeoIp2\InvalidArgumentException;

try {
     var_dump(GeoIp2::getLocation('8.8.8.8')); // ipv4
     var_dump(GeoIp2::getLocation('::0808:0808')); // ipv6
//     [
//         'country' => 'United States',
//         'city' => '--',
//         'asn' => 'GOOGLE',
//         'continent' => 'North America',
//         'timezone' => 'America/Chicago',
//     ]
 }catch (InvalidArgumentException $exception){
 
 }

use Larva\GeoIp2\GeoIp2;
use Larva\GeoIp2\InvalidArgumentException;

try {
    var_dump(GeoIp2::city('8.8.8.8')); // ipv4
    var_dump(GeoIp2::city('::0808:0808')); // ipv6
    // 返回 GeoIp2\Model\City 对象
    
 }catch (IpAttributionException $exception){
 
 }

use Larva\GeoIp2\GeoIp2;
use Larva\GeoIp2\InvalidArgumentException;

try {
    var_dump(GeoIp2::country('8.8.8.8')); // ipv4
    var_dump(GeoIp2::country('::0808:0808')); // ipv6
    // 返回 GeoIp2\Model\Country 对象
    
 }catch (IpAttributionException $exception){
 
 }

use Larva\GeoIp2\GeoIp2;
use Larva\GeoIp2\InvalidArgumentException;

try {
    var_dump(GeoIp2::asn('8.8.8.8')); // ipv4
    var_dump(GeoIp2::asn('::0808:0808')); // ipv6
    // 返回 GeoIp2\Model\Asn 对象
    
 }catch (IpAttributionException $exception){
 
 }

use Larva\GeoIp2\GeoIp2;
use Larva\GeoIp2\GeoIp2Manager;
use Larva\GeoIp2\InvalidArgumentException;

var_dump(GeoIp2::createReader(GeoIp2Manager::DB_CITY)); // City库
// 返回连接City库的 GeoIp2\Database\Reader 对象
var_dump(GeoIp2::createReader(GeoIp2Manager::DB_ASN)); // ASN库
// 返回连接ASN库的 GeoIp2\Database\Reader 对象   
var_dump(GeoIp2::createReader(GeoIp2Manager::DB_ASN)); // Country库
// 返回连接Country库的 GeoIp2\Database\Reader 对象