geocoder-php/stack-geo-ip

Geocoder库的地理位置中间件。

1.0.3 2014-10-28 15:06 UTC

This package is auto-updated.

Last update: 2024-08-29 03:06:55 UTC


README

通过利用Geocoder库,为后续中间件添加地理位置结果的Stack中间件。

Build Status

使用方法

示例

在此,我们创建了一个简单的应用程序,它将请求的IP地址和X-Country头的内容返回给浏览器。通常,X-Country头不会存在。

通过使用StackBuilder将GeoIP中间件包装在它周围,如果IP地址可以匹配到国家,则将X-Country头设置为该国家的两字母代码,并可供应用程序使用。

<?php

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;

require __DIR__ . '/vendor/autoload.php';

$app = new \Silex\Application();

$app->get('/', function(Request $request) {
    $ip      = $request->getClientIp();
    $country = $request->headers->get('X-Country', 'UNKNOWN');

    return new Response($ip . ' => '. $country, 200);
});

$stack = (new \Stack\Builder())
    ->push('Geocoder\Stack\GeoIp')
    ;

$app = $stack->resolve($app);

$request  = Request::createFromGlobals();
$response = $app->handle($request)->send();
$app->terminate($request, $response);

选项

可以使用以下选项

  • adapter (可选): 要使用的Geocoder HTTP适配器。默认为cURL适配器。

  • provider (可选): 要使用的Geocoder提供者。默认为FreeGeoIP提供者。对于生产环境,建议使用依赖于本地文件而不是HTTP请求的提供者,例如Max Mind二进制提供者。

  • header (可选): 将国家结果存储的HTTP头名称。默认为"X-Country"。

请参阅Geocoder文档以获取可用适配器和提供者的列表。

安装

推荐通过Composer安装StackGeoIp

{
    "require": {
        "geocoder-php/stack-geo-ip": "@stable"
    }
}

提示:您应该浏览geocoder-php/stack-geo-ip页面以选择要使用的稳定版本,避免使用@stable元约束。

许可证

StackGeoIp在MIT许可证下发布。有关详细信息,请参阅捆绑的LICENSE文件。