PHP code example of workbunny / webman-ip-attribution

1. Go to this page and download the library: Download workbunny/webman-ip-attribution 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/ */

    

workbunny / webman-ip-attribution example snippets


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

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

use Workbunny\WebmanIpAttribution\Location;

$location = new Location([
    'default'  => '--',      // 缺省展示值
    'language' => ['zh-CN'], // 语言
    'db-country' => null,    // 自定义的country库绝对地址
    'db-city'    => null,    // 自定义的city库绝对地址
    'db-asn'     => null,    // 自定义的asn库绝对地址
]);

use Workbunny\WebmanIpAttribution\Location;
use Workbunny\WebmanIpAttribution\Exceptions\IpAttributionException

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

use Workbunny\WebmanIpAttribution\Location;
use Workbunny\WebmanIpAttribution\Exceptions\IpAttributionException

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

use Workbunny\WebmanIpAttribution\Location;
use Workbunny\WebmanIpAttribution\Exceptions\IpAttributionException

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

use Workbunny\WebmanIpAttribution\Location;
use Workbunny\WebmanIpAttribution\Exceptions\IpAttributionException

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

use Workbunny\WebmanIpAttribution\Location;

$location = new Location();
var_dump($location->createReader(Location::DB_CITY)); // City库
// 返回连接City库的 GeoIp2\Database\Reader 对象
var_dump($location->createReader(Location::DB_ASN)); // ASN库
// 返回连接ASN库的 GeoIp2\Database\Reader 对象   
var_dump($location->createReader(Location::DB_ASN)); // Country库
// 返回连接Country库的 GeoIp2\Database\Reader 对象