greenpeace-cee/maxmind-geoip

MaxMind GeoIP Bundle

安装: 28

依赖关系: 0

建议者: 0

安全性: 0

星星: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v1.0.6 2024-08-20 08:30 UTC

This package is auto-updated.

Last update: 2024-09-20 08:37:42 UTC


README

这是一个用于轻松使用 MaxMind GeoIp Bundle 的 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 Free Library 的导入,您可以在 http://www.maxmind.com/ 找到。