新浪通过IP地址获取当前地理位置(ip端、国家、省份、城市、运营商)的php接口很强大呢
/**
* 取得地理位置信息
* $format : js json string
* return $array
*/
function iplookup($format='json'){
//返回Sina地理位置信息
$res = restRequest('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format='.$format);
$res = json_decode($res,true);
return $res;
}
/**
* 调用REST服务
*/
function restRequest($url, $method = 'GET', $data, $contentType = 'application/json', $timeout = 30, $proxy = false) {
$ch = null;
if ('POST' === strtoupper($method)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER,0 );
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if($contentType) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
}
if(is_string($data)){
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
} elseif('GET' === strtoupper($method)) {
if(is_string($data)) {
$real_url = $url. (strpos($url, '?') === false ? '?' : ''). $data;
} else {
$real_url = $url. (strpos($url, '?') === false ? '?' : ''). http_build_query($data);
}
$ch = curl_init($real_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
} else {
$args = func_get_args();
//Log::error($args);
return false;
}
if ($proxy) {
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
$ret = curl_exec($ch);
$info = curl_getinfo($ch);
$contents = array(
'httpInfo' => array(
'send' => $data,
'url' => $url,
'ret' => $ret,
'http' => $info,
)
);
curl_close($ch);
return $ret;
}