新浪通过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;
}
ad:真正免费,域名+虚机+企业邮箱=0元
