//token 验证是通过的,但是关注事件的反馈不成功,查了几遍代码,改了几个错误,还是不行。
<?php
class IndexAction extends Action {
protected function _initialize() {
header("Content-Type:text/html; charset=utf-8");
}
public function index(){
//获得参数 signature nonce token timestamp echostor
$nonce =$_GET['nonce'];
$token ='we';
$timestamp =$_GET['timestamp'];
$echostr =$_GET['echostr'];
$signature =$_GET['signature'];
//形成数组,然后按字典序排列
$array =array();
$array =array($nonce,$timestamp,$token);
sort($array);
//拼接成字符串,加密,然后与signature校验
$str =sha1(implode($array));
if($str==$signature && $echostr){
//第一次介入微信API接口,之后就没有$echostr这个参数了
echo $echostr;
exit;
}else{
$this->reponseMsg();
}
}
//接收事件推送并回复
public function reponseMsg(){
//1.获取到微信送过来的post数据(xml形式)
$postArr =$GLOBALS['HTTP_RAW_POST_DATA'];
//2.处理消息类型,并设置回复类型和内容
$postObj =$simplexml_load_string($postArr);
//$postObj->ToUserName = '';
//$postObj->FromUserName = '';
//$postObj->CreateTime = '';
//$postObj->Msgtype = '';
//$postObj->Event = '';
//判断该数据包是否是消息订阅的时间推送
if(strtolower($postObj->MsgType == 'event'){//strtolower将字符串转换成小写形式
//如果是关注事件subscribe 事件
if(strtolower($postObj->Event == 'subscribe')){
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$Msgtype = 'text';
$Content = '欢迎关注我们的公众账号,zxy';
$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;
}
}
}
}
?>