maxmind/geoip

MaxMind GeoIP Symfony2 扩展包

安装量: 1,267,242

依赖关系: 2

建议者: 0

安全性: 0

星标: 27

关注者: 6

分支: 34

类型:symfony-bundle

v1.1.5 2018-02-07 23:32 UTC

This package is auto-updated.

Last update: 2024-08-24 20:21:37 UTC


README

一个用于轻松使用 Maxmind GeoIp 扩展包的 Symfony 扩展包。

SensioLabsInsight Build

安装

要安装此库,请按照以下步骤操作

首先将依赖项添加到您的 composer.json 文件中

"require": {
    ...
    "maxmind/geoip": "dev-master"
},

然后使用以下命令安装扩展包

php composer update

在您的应用程序内核中启用扩展包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Maxmind\Bundle\GeoipBundle\MaxmindGeoipBundle(),
    );
}

现在库已安装。

要获取 Maxmind 数据源文件('.dat' 格式),您可以选择以下两种方法之一

您可以去 Maxmind 免费下载数据页面: http://dev.maxmind.com/geoip/geolite 获取所需版本。然后您需要将下载的文件解压缩到位于 'vendor/maxmind/geoip/data' 的数据目录中。

或者您可以直接执行以下命令

php app/console maxmind:geoip:update-data %url-data-source%

将 %url-data-source% 替换为所需数据源的 URL。例如: http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

如果您想在其他目录中使用您的数据文件,您可以在 app\config\config.yml 中进行配置

# app/config/config.yml
maxmind_geoip:
	data_file_path: "%kernel.root_dir%/../web/GeoIPCity.dat"

现在您可以在 Symfony2 应用程序中的任何地方使用 Maxmind GeoIp 库。

用法

以下示例适用于您在控制器中

$geoip = $this->get('maxmind.geoip')->lookup(%IP_ADDR%);

$geoip->getCountryCode();
$geoip->getCountryCode3();
$geoip->getCountryName();
$geoip->getRegion();
$geoip->getCity();
$geoip->getPostalCode();
$geoip->getLatitude();
$geoip->getLongitude();
$geoip->getAreaCode();
$geoip->getMetroCode();
$geoip->getContinentCode();

或在 twig 文件中

{{ ip|geoip.countryCode }}
{{ ip|geoip.countryCode3 }}
{{ ip|geoip.countryName }}
{{ ip|geoip.regionCode }}
{{ ip|geoip.region }}
{{ ip|geoip.city }}
{{ ip|geoip.postalCode }}
{{ ip|geoip.latitude }}
{{ ip|geoip.longitude }}
{{ ip|geoip.areaCode }}
{{ ip|geoip.metroCode }}
{{ ip|geoip.continentCode }}

您可以在 'routing_dev' 中添加一个演示路由来获取此扩展包如何工作的示例,例如

_maxmind_geoip:
    resource: "@MaxmindGeoipBundle/Controller/DemoController.php"
    type:     annotation
    prefix:   /demo

在 /demo/geoip 获取查找

此库是 Maxmind GeoIp 免费库的导入,您可以在 http://www.maxmind.com/ 找到