<?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;
$customrevent = $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 ($customrevent=="subscribe")
{$contentStr = "感谢你的关注\n回复1查看联系方式\n回复2查看最新资讯\n回复3查看法律文书";}
break;
case "image";
$contentStr = "你的图片很棒!";
break;
case "location";
$geourl1="http://api.map.baidu.com/telematics/v2/distance?waypoints=116.681837,23.353299;{$longitude},{$latitude}&ak=1a3cde429f38434f1811a75e1a90310c";
$apistr1=file_get_contents($geourl1);
$apiobj1=simplexml_load_string($apistr1);
$distanceobj1=$apiobj1->results->distance;
$distanceint1=intval($distanceobj1); //第一个店铺的距离
$geourl2="http://api.map.baidu.com/telematics/v2/distance?waypoints=116.756091,23.46596;{$longitude},{$latitude}&ak=1a3cde429f38434f1811a75e1a90310c";
$apistr2=file_get_contents($geourl2);
$apiobj2=simplexml_load_string($apistr2);
$distanceobj2=$apiobj2->results->distance;
$distanceint2=intval($distanceobj2); //第二个店铺的距离
$geourl3="http://api.map.baidu.com/telematics/v2/distance?waypoints=116.372831,23.549993;{$longitude},{$latitude}&ak=1a3cde429f38434f1811a75e1a90310c";
$apistr3=file_get_contents($geourl3);
$apiobj3=simplexml_load_string($apistr3);
$distanceobj3=$apiobj3->results->distance;
$distanceint3=intval($distanceobj3);//第三个店铺的距离
$mindistanceint=min($distanceint1,$distanceint2,$distanceint3);//最近的店铺距离
if($mindistanceint==$distanceint1){
$contentStr = "离你最近的店铺有{$mindistanceint}米远,位置是1号路";}
elseif ($mindistanceint==$distanceint2){
$contentStr = "离你最近的店铺有{$mindistanceint}米远,位置是2号路";}
else{
$contentStr = "离你最近的店铺有{$mindistanceint}米远,位置是3号路";}
break;
case "link" ;
$contentStr = "你的链接有病毒吧!";
break;
case "text";
switch($keyword)
{
case "1";
$contentStr = "联系方式:汕头市金平区华乾大厦703,1341702551, email:[email protected]";
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;
}
}
}
?>
微信开发PHP代码最近店铺
猜你喜欢
转载自blog.csdn.net/qiphon3650/article/details/80716359
今日推荐
周排行