d1f8bd40
Alexey Boroda
first commit
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
Configuration
---
1) In your config/components.php
```php
'geolocation' => [
'class' => \frontend\modules\location\components\geolocation\Geolocation::class,
'config' => [
'provider' => 'geoplugin',
'return_formats' => 'php'
],
],
```
### Config params
```php
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.
```
### Plugins
Plugins are simple PHP files, that returns an array with three vars:
- plugin_url : URL of webservice, with three special tags:
a) {{accepted_formats}}
b) {{ip}}
c) {{api_key}}
These tags will be replaced by their respective values.
- accepted_formats : An array with the return acceptable formats (example ['csv', 'php', 'json', 'xml'])
- default_accepted_format : String with the default return format. (example "php")
### Plugin file example
```php
<?php
$plugin = [
'plugin_url' => 'http://www.geoplugin.net/{{accepted_formats}}.gp?ip={{ip}}',
'accepted_formats' => ['json', 'php', 'xml'],
'default_accepted_format' => 'php',
];
```
### How to use
In your view:
```php
<?php
print_r(yii::$app->geolocation->getInfo());
?>
```
or, to find the geolocation infos from Google server, on your view.
```php
<?php
print_r(yii::$app->geolocation->getInfo('173.194.118.22'));
?>
```
To change the plugin
--------------------
```php
<?php
yii::$app->geolocation->getPlugin('ippycox','XML');
print_r(yii::$app->geolocation->getInfo('173.194.118.22'));
?>
```
### What you get?
Using the geoplugin provider:
```php
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
)
```
For more information, please visit http://www.geoplugin.com/
Using the freegeoip provider:
```php
{
"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
}
```
For more information, please visit https://freegeoip.net/
For another plugins infos, please use the sources. :)
### Redirect by browser language
Using
```php
\Yii::$app->geolocation->redirectOnBrowserLanguage();
```
|