我查不出代码哪里错了/(ㄒoㄒ)/~~就是不成功,好心人帮我看看

来源:3-1 接收事件订阅与回复响应消息(一)

娃娃爱亲亲

2015-12-05 22:51

//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;
            }
        }
    }
    

}

?>


写回答 关注

5回答

  • merlin1001
    2016-12-07 15:57:13

    $postObj =$simplexml_load_string($postArr);这个地方不对的

  • Paitson
    2016-08-15 16:34:08

    同问啊!

  • 请叫我陛下
    2016-07-28 16:26:41

    不知道哪里出问题了,关注了以后也不出现回复

  • 请叫我陛下
    2016-07-28 16:25:01

    一样啊,我也是这样的问题每次this->reponseMsg();这个有错误

  • 娃娃爱亲亲
    2015-12-05 23:57:35

    补充:我查到是this->reponseMsg();这句有符号错误,然后改成了this.reponseMsg();这个时候符号错误没有了,又出现说reponseMsg()函数未定义,我明明后面写了public function reponseMsg(){}啊!天啊,这是为什么?

PHP实现微信公众平台开发—提升篇

本课程详细讲解了接收微信用户发送的消息和接收订阅关注的事件推送

64948 学习 · 371 问题

查看课程

相似问题