tantacula/ip2country

Laravel 包,用于查找与 IPv4 地址关联的国家(由 'Smalldogs' 仓库分支而来)

1.1.0 2014-11-20 00:00 UTC

This package is not auto-updated.

Last update: 2024-09-21 09:55:39 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