用百度地图api地址获取位置
http://api.map.baidu.com/telematics/v2/reverseGeocoding?location={$longitude},{$latitude}&ak=1a3cde429f38434f1811a75e1a90310c";
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$type = $postObj->MsgType;
$customevent = $postObj->Event;
$latitude = $postObj->Location_X;
$longitude = $postObj->Location_Y;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
switch ($type)
{ case "event";
if ($customevent=="subscribe")
{$contentStr = "感谢你的关注\n回复1查看联系方式\n回复2查看最新资讯\n回复3查看法律文书";}
break;
case "image";
$contentStr = "你的图片很棒!";
break;
case "location";
$geourl="http://api.map.baidu.com/telematics/v2/reverseGeocoding?location={$longitude},{$latitude}&ak=1a3cde429f38434f1811a75e1a90310c";//反Geocoding接口
$apistr=file_get_contents($geourl);//读取文件
$apiobj=simplexml_load_string($apistr);//xml解析
$addstr=$apiobj->results->result[0]->name;//逐级解析
$contentStr = "我知道你在{$addstr}附近";
break;
case "link" ;
$contentStr = "你的链接有病毒吧!";
break;
case "text";
switch($keyword)
{
case "1";
$contentStr = "联系方式:汕头市金平区华乾大厦703,1341702551, email:30090032@qq.com";
break;
case "2";
$contentStr = "最新资讯:《汕头市环境违法行为举报奖励办法》将于8月1日实施。";
break;
case "3";
$contentStr = "31离婚协议\n32租房合同\n33交通事故协议书";
break;
default;
$contentStr ="hi";
}
break;
default;
$contentStr ="此项功能尚未开发";
}
$msgType="text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else {
echo "";
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>