这里调用的是百度的开放查询接口,通过网页的方式去查询然后从返回的结果中去匹配我们所需要的信息,效果还行,代码在下面,还是能凑合着用的
var_dump(getAddress('202.101.98.54','福州'));
02
03 function getAddress($ip=null,$address='') {
04 $url = "http://open.baidu.com/ipsearch/s?wd={$ip}&tn=baiduip";
05 $res = mb_convert_encoding(HttpRequest($url), 'UTF-8', 'GBK');
06 //var_dump($res);
07 if ( preg_match('#来自:<b>(.+)</b>#Ui', $res, $m) ) {
08 if (is_int(strpos($m[1], $address))) {
09 return $m[1];
10 }
11 }
12 }
13
14
15 function HttpRequest($url, $data=array()) {
16 $ch = curl_init();
17 if (is_array($data) && $data) {
18 $formdata = http_build_query($data);
19 curl_setopt($ch, CURLOPT_POST, true);
20 curl_setopt($ch, CURLOPT_POSTFIELDS, $formdata);
21 }
22 curl_setopt($ch, CURLOPT_URL, $url);
23 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
24 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
25 curl_setopt($ch, CURLOPT_TIMEOUT, 2);
26 return curl_exec($ch);
27 }