普通IP定位 API是一个根据IP返回对应大致位置信息的HTTP/HTTPS形式位置服务接口,支持多种语言调用,如C# 、C++、Java等,即通过发送HTTP/HTTPS请求,返回JSON格式的位置数据(包括经纬度信息、地址信息等)。
该接口免费对外开放,您需先申请密钥(AK)才可使用。每个开发者帐号每天使用该服务的次数是有限的,若需更多的使用次数,请完成开发者认证以获取更高配额和更及时的技术支持。
功能介绍
调用API接口,返回请求参数中指定IP的大致位置信息,包括:经纬度、省、市、区县等地址信息。
如果请求参数中的IP为空,则默认返回当前服务请求来源的IP所对应的大致位置信息。
适用场景
以城市为分类的应用或网站:根据用户当前IP来提供对应城市的服务。
普通IP定位 API详见官网:http://lbsyun.baidu.com/index.php?title=webapi/ip-api
高精度IP定位 API详见官网:http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ip
下面代码是普通IP定位的
<?
header("Content-type: text/html; charset=utf-8");
define("TOKEN","自填");
define("APPID","自填");
define("APPSECRET","自填");
$wechatObj = new wechatCallbackapiTest();
if(!isset($_GET["echostr"])){
$wechatObj -> responseMsg();
}else{
$wechatObj ->valid();
}
class wechatCallbackapiTest{
public function valid(){
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
private function checkSignature(){
if(!defined("TOKEN")){
throw new Exception();
}
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token,$timestamp,$nonce);
sort($tmpArr,SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature){
return true;
}else{
return false;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch ($RX_TYPE)
{
case "event":
$result = $this->receiveEvent($postObj);
break;
case "text":
$result = $this->receiveText($postObj);
break;
case "image":
$result = $this->receiveImage($postObj);
break;
case "location":
$result = $this->receiveLocation($postObj);
break;
case "voice":
$result = $this->receiveVoice($postObj);
break;
case "video":
case "shortvideo":
$result = $this->receiveVideo($postObj);
break;
case "link":
$result = $this->receiveLink($postObj);
break;
default:
$result = "unknown msg type: ".$RX_TYPE;
break;
}
echo $result;
}else {
echo "";
exit;
}
}
private function receiveEvent($object)
{
include("locationinfoUpload.php");
$resInsert = insertLocationinfo($object);
$content = "";
switch ($object->Event)
{
case "subscribe":
$content = "欢迎关注通州-羽我同行俱乐部!\n请回复以下关键字:文本 表情 单图文 多图文 音乐\n请按住说话 或 点击 + 再分别发送以下内容:语音 图片 小视频 我的收藏 位置";
$content .= (!empty($object->EventKey))?("\n来自二维码场景 ".str_replace("qrscene_","",$object->EventKey)):"";
$content .= "\n\n".'<a href="羽我同行.cn">技术支持 大米</a>';
break;
case "unsubscribe":
$content = "取消关注";
break;
case "CLICK":
switch ($object->EventKey)
{
case "78":
$content = array();
$content[] = array(
"Title"=>"天气在手,羽球无忧,输入“天气北京”",
"Description"=>