慕设计6780568
2019-03-03 15:26
//获得参数 signature nonce token timestamp echostr
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$token = 'hua';
$signature = $_GET['signature'];
$echostr = $_GET['echostr']; //形成数组,然后按字典序排序
$array = array();
$array = array( $nonce, $timestamp, $token);
sort( $array); //拼接成字符串,sha1加密,然后与signature进行校验
$tmpstr = sha1(implode( $array ));
if ( $tmpstr == $signature && $echostr ) //第一次接入微信api接口的时候
{ echo $echostr;
exit;
} else { // $this->reponseMsg(); }
// function reponseMsg(){
//1.获取到微信推送过来post数据(xml格式)
$postArr = $GlOBALS['HTTP_RAW_POST_DATA'];
//2.处理消息类型,并设置回复类型和内容
$postObj = simplexml_load_string( $postArr );
//判断该数据包是否是订阅事件的推送
if( strtolower( $postObj->MsgType) == 'event'){
//如果是关注subscribe 事件
if ( strtolower($postObj->Event == 'subscribe') )
{ //回复用户消息
$toUser = $postObj->FromUserName;
$fromUser= $postObj->ToUserName;
$time = time();
$msgType = 'text';
$content = '谢谢小可爱的关注';
$template= "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
echo $info
}
}
}
这是代码
还没有人回答问题,可以看看其他问题
PHP实现微信公众平台开发—提升篇
64945 学习 · 262 问题
相似问题