rodzadra/geolocation

一个简单的Yii2组件,用于查找地理客户端信息。

安装量: 16,404

依赖关系: 1

建议者: 0

安全性: 0

星标: 6

关注者: 2

分支: 5

开放问题: 0

类型:yii2-extension

dev-master 2016-02-29 16:38 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:33:22 UTC


README

一个简单的Yii2组件,用于查找地理客户端信息。

Yii2

安装

安装

安装此扩展的首选方式是通过composer

运行以下命令之一:

composer require --prefer-dist rodzadra/geolocation "*"

或者

"rodzadra/geolocation": "*"

将以下内容添加到您的composer.json文件的require部分。

配置

  1. 在您的config/main.php中
'components'=>[
    'geolocation' => [ 
        'class' => 'rodzadra\geolocation\Geolocation',
        'config' => [
            'provider' => '[PLUGIN_NAME]',
            'return_formats' =>  '[SUPORTED_PLUGIN_FORMATS]',
            'api_key' => '[YOUR_API_KEY],
        ],
    ],
],

配置参数

provider - The name of plugin to use (see examples on @vendor/rodzadra/geolocation/plugins/);

return_formats - The return formats supported by the plugin

api_key - If necessary, you can pass your api key here.

插件

插件是简单的PHP文件,返回一个包含三个变量的数组

  • plugin_url : 包含三个特殊标记的webservice URL

a) {{accepted_formats}}

b) {{ip}}

c) {{api_key}}

这些标记将被相应的值替换。

  • accepted_formats : 一个包含可接受返回格式的数组(例如 ['csv', 'php', 'json', 'xml'])

  • default_accepted_format : 包含默认返回格式的字符串(例如 "php")

插件文件示例

<?php

$plugin = [
            'plugin_url'                => 'http://www.geoplugin.net/{{accepted_formats}}.gp?ip={{ip}}',
            'accepted_formats'          => ['json', 'php', 'xml'],
            'default_accepted_format'   => 'php',
    ];

如何使用

在您的视图中

<?php
  print_r(yii::$app->geolocation->getInfo());
?>

或,在您的视图中从Google服务器查找地理定位信息。

<?php
  print_r(yii::$app->geolocation->getInfo('173.194.118.22'));
?>

更改插件

<?php
    yii::$app->geolocation->getPlugin('ippycox','XML');
  print_r(yii::$app->geolocation->getInfo('173.194.118.22'));
?>

您将获得什么?

使用geoplugin提供者

Array
(
    [geoplugin_request] => 173.194.118.22
    [geoplugin_status] => 200
    [geoplugin_credit] => Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.
    [geoplugin_city] => Mountain View
    [geoplugin_region] => CA
    [geoplugin_areaCode] => 650
    [geoplugin_dmaCode] => 807
    [geoplugin_countryCode] => US
    [geoplugin_countryName] => United States
    [geoplugin_continentCode] => NA
    [geoplugin_latitude] => 37.419201
    [geoplugin_longitude] => -122.057404
    [geoplugin_regionCode] => CA
    [geoplugin_regionName] => California
    [geoplugin_currencyCode] => USD
    [geoplugin_currencySymbol] => $
    [geoplugin_currencySymbol_UTF8] => $
    [geoplugin_currencyConverter] => 1
)

更多信息,请访问http://www.geoplugin.com/

使用freegeoip提供者

{
 "ip":"173.194.118.22",
 "country_code":"US",
 "country_name":"United States",
 "region_code":"CA",
 "region_name":"California",
 "city":"Mountain View",
 "zip_code":"94043",
 "time_zone":"America/Los_Angeles",
 "latitude":37.419,
 "longitude":-122.058,
 "metro_code":807
}

更多信息,请访问https://freegeoip.net/

有关其他插件的更多信息,请使用源代码。 :)