Ip2address.class.php
10.2 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* ip2address.php
* IP地址转换地理位置类
* Create By Deepseath
* $Author$
* $Id$
*/
namespace Org\Net;
use Think\Log;
class Ip2address
{
/**
* 返回的结果
*/
public $result = array(
'address' => '', // 完整地址信息字符串
'address_list' => array(), // 地址信息数组
'source' => ''
); // 接口获取到的原始数据
/**
* 错误编码
*/
public $errcode = 0;
/**
* 错误描述
*/
public $errmsg = '';
/**
* 淘宝ip库接口url
*/
const URI_TAOBAO = 'http://ip.taobao.com/service/getIpInfo.php?ip=%s';
/**
* 首选使用的服务:local=本地ip库、taobao=淘宝ip库
*/
protected $_first_service = 'taobao';
/**
* 待查询的IP地址
*/
protected $_ip = '';
/**
* 请求的url
*/
protected $__request_url = '';
/**
* Snoopy类
*/
protected $__snoopy = null;
public function __construct($first_service = 'taobao')
{
$this->_first_service = $first_service;
}
/**
* 获取IP所在地
*
* @param string $ip
* @return boolean
*/
public function get($ip)
{
$this->_ip = $ip;
$validator = new \Com\Validator();
if (! $validator->is_ip($this->_ip)) {
return false;
}
// 多个获取方式
$get_methods = array(
'local',
'taobao'
);
// 首选获取服务方法
$method = '_get_by_' . $this->_first_service;
// 临时的错误代码和错误消息
$errcode = 0;
$errmsg = '';
$result = '';
// 使用首选服务出错,则尝试其他服务
if (! $this->$method()) {
// 将首选服务的错误信息输出
$errcode = $this->errcode;
$errmsg = $this->errmsg;
// 遍历其他方法获取
foreach ($get_methods as $_method) {
if (rstrtolower($_method) == rstrtolower($this->_first_service)) {
// 由于已过使用首选方法,则忽略
continue;
}
$method = '_get_by_' . $_method;
if ($this->$method()) {
// 获取成功则退出
break;
}
}
}
// 输出首选服务的错误
if ($errcode) {
$this->errcode = $errcode;
$this->errmsg = $errmsg;
}
return true;
}
/**
* TODO 本地解析方式尚未实现
* 通过本地纯真库获取IP所在地位置
*
* @return boolean
*/
protected function _get_by_local()
{
$this->errcode = 100;
$this->errmsg = '尚未实现本地化';
return false;
}
/**
* 通过淘宝IP API接口获取地址
*
* @return boolean
*/
protected function _get_by_taobao()
{
// 将ip地址变量赋值
$url = sprintf(self::URI_TAOBAO, $this->_ip);
// 初始化结果
$sdata = array();
if (! $this->_get_json_by_httprequest($sdata, $url)) {
$this->errcode = 1002;
$this->errmsg = '解析TAOBAO数据发生错误';
// Log::record('taobao ip api error: '.$url.'|'.$this->errcode.'|'.$this->errmsg);
Log::record('taobao ip api error: ' . $url . '|' . $this->errcode . '|' . $this->errmsg);
return false;
}
// 无法获取结果集中的返回码,可能该接口发生变动
if (! isset($sdata['code'])) {
$this->errcode = '1003';
$this->errmsg = '获取 IP 位置接口错误';
Log::record('taobao ip api error: ' . $url . '|' . $this->errcode . '|' . $this->errmsg);
return false;
}
// 结果集中返回码不为空,则获取地址发生解析错误
if (! empty($sdata['code'])) {
$this->errcode = '1002';
$this->errmsg = '获取 IP 位置信息发生错误(code:' . $sdata['code'] . ')';
Log::record('tobao ip api error:' . $url . '|' . $this->errcode . '|' . $this->errmsg . '|' . $sdata['code'] . '|' . $sdata['data']);
return false;
}
// 如果结果集中数据为空,则可能出现意外的错误
if (empty($sdata['data'])) {
$this->errcode = 1004;
$this->errmsg = '获取 IP 位置信息发生错误';
Log::record('taobao ip api error:' . $url . '|' . $this->errcode . '|' . $this->errmsg);
return false;
}
// 地址信息原始结果数组
$data = $sdata['data'];
// 地址信息数组
$address_list = array(
'country' => '',
'region' => '',
'city' => '',
'county' => ''
);
// 地址信息字符串
$address = '';
// 国家
if (! empty($data['country']) && is_scalar($data['country'])) {
$address_list['country'] = trim($data['country']);
if (! isset($data['country_id']) || rstrtolower($data['country_id']) != 'cn') {
// 不显示“中国”
$address .= $address_list['country'];
}
}
// 省份
if (! empty($data['region']) && is_scalar($data['region'])) {
$address_list['region'] = trim($data['region']);
$address .= $address_list['region'];
}
// 城市
if (! empty($data['city']) && is_scalar($data['city'])) {
$address_list['city'] = trim($data['city']);
// 省份与城市不一致则显示,主要用于直辖市不重复显示
if ($address_list['city'] != $address_list['region']) {
$address .= $address_list['city'];
}
}
// 县
if (! empty($data['county']) && is_scalar($data['county'])) {
$address_list['county'] = trim($data['county']);
$address .= $address_list['county'];
}
// 输出结果
$this->result = array(
'address' => $address,
'address_list' => $address_list,
'source' => $data
);
return true;
}
/**
* 通过http请求获取json结果数据
*
* @param string $data
* <strong style="color:red">(引用结果)</strong>返回的结果
* @param string $url
* 请求的url
* @param string $post
* 请求的数据
* @param array $http_header
* 请求的header头信息
* @param string $http_method
* 请求方法
* @param object $snoopy_reporting
* <strong style="color:red">(引用结果)</strong>snoopy信息
* @return boolean
*/
protected function _get_json_by_httprequest(&$data, $url, $post = array(), $http_header = array(), $http_method = 'GET', &$snoopy_reporting = array())
{
// 初始化结果数据
$data = '';
// 通过网络读取数据
$http_data = '';
if ($this->__httprequest($http_data, $url, $post, $http_header, $http_method, $snoopy_reporting) === false) {
return false;
}
// 尝试解析json
$data = @json_decode(trim($http_data), true);
// 解析失败
if ($data === null) {
$this->errcode = 1003;
$this->errmsg = '解析JSON数据发生错误';
Log::record('json parse error: ' . $this->__request_url . '|' . $http_data . '|' . $this->errcode . '|' . $this->errmsg);
return false;
}
return true;
}
/**
* http请求私有方法
*
* @param string $data
* <strong style="color:red">(引用结果)</strong>请求结果
* @param string $url
* 请求的url
* @param string $post
* 请求的数据
* @param array $http_header
* 请求的header头信息
* @param string $http_method
* 请求方法
* @param object $snoopy_reporting
* <strong style="color:red">(引用结果)</strong>snoopy信息
* @return boolean
*/
protected function __httprequest(&$data, $url, $post = array(), $http_header = array(), $http_method = 'GET', &$snoopy_reporting = array())
{
// 初始化结果数据
$data = '';
$this->__request_url = $url;
// 载入 Snoopy 类
if ($this->__snoopy === null) {
$this->__snoopy = new \Org\Net\Snoopy();
}
// 使用自定义的头字段,格式为 array(字段名 => 值, ... ...)
if (! empty($http_header) && is_array($http_header)) {
$this->__snoopy->rawheaders = $http_header;
}
switch (rstrtoupper($http_method)) {
case 'POST': // 使用 POST 协议
case 'PUT': // 使用 PUT 协议
$result = $this->__snoopy->submit($url, $post);
break;
case 'DELETE': // 使用 DELETE 协议
$result = $this->__snoopy->submit_by_delete($url, $post);
break;
default: // 使用 GET 协议
if ($post) {
if (is_array($post)) {
$get_data = http_build_query($post);
} else {
$get_data = $post;
}
if (strpos($url, '?') === false) {
$url .= '?';
} else {
$url .= '&';
}
$url .= $get_data;
}
$result = $this->__snoopy->fetch($url);
}
// 将Snoopy信息返回,用于调试
$snoopy_reporting = $this->__snoopy;
// 如果读取错误
if (! $result || 200 != $this->__snoopy->status) {
$this->errcode = 1004;
$this->errmsg = '请求接口发生网络错误';
Log::record('$snoopy->submit error: ' . $url . '|' . $result . '|' . $this->__snoopy->status . '|' . $this->errcode . '|' . $this->errmsg);
return false;
}
// 返回结果数据
$data = $this->__snoopy->results;
return true;
}
}