smalldogs/ip2country

此包已 弃用 并不再维护。未建议替代包。

Laravel 包,用于查找与 IPv4 地址关联的国家

1.1.0 2014-11-20 00:00 UTC

This package is not auto-updated.

Last update: 2017-08-21 08:50:13 UTC


README

Downloads License Version TravisCI

Laravel 包,用于查找与 IPv4 地址关联的国家。注重轻量级和快速查找。创建并填充本地数据库表,因此运行时不会进行外部请求。

此包包含由 MaxMind 创建的 GeoLite 数据,可在 http://www.maxmind.com 获取。更新后的免费可下载数据库每月第一个星期二发布。我会尽力确保每次都更新此包。

当前 IP 映射表

似乎每月变化不大。当前版本由 MaxMind 于 2015年2月4日 发布

如何更新映射数据库表

如果您已经安装并设置了 ip2country 包,但想要更新 IP 数据库映射。首先,获取最新的 ip2country 包。

composer update 

或者,仅更新此包

composer update smalldogs/ip2country

然后,通过运行新的迁移来更新您的数据库。

php artisan migrate --package="smalldogs/ip2country"

如何安装

1. 使用 Composer 需求此包。
composer require "smalldogs/ip2country"
2. 创建并填充数据库查找表。
php artisan migrate --package="smalldogs/ip2country"
3. 将服务添加到您的 app/config/app.php 中的 providers 数组。
'providers' => array(
        'Smalldogs\Ip2Country\Ip2CountryServiceProvider',
        //[...]
);

如何使用

// Returns the 2 letter country code for the user, eg: 'US'
$myCountryCode = Ip2Country::get();

// Returns the full name of the country, eg: 'United States'
$myCountryName = Ip2Country::getFull();

// Get the country for someone other than user on the page
$someonesIpAddress = '192.168.0.1';
$someonesCountryCode = Ip2Country::get($someonesIpAddress);

配置

默认情况下,如果 IP 地址在查找表中未找到,它将返回国家代码 'US',国家名称为 'United States'。您可以在配置中进行自定义。

php artisan config:publish smalldogs/ip2country

然后导航到 app/config/packages/smalldogs/ip2country/config.php

return array(
    // If IP address is not found, what country is returned
    'default_country_code' => 'US',
    'default_country_name' => 'United States',
    
    // Since our data will change, at most, once a month. Cache the Ip lookup for a day
    // Default: 1 day. Set to 0 or null to disable.
    'cache_results' => 60 * 24
);

如果您不想在找不到 IP 地址时返回任何内容,只需将这些值都设置为 => null