关注事件文本回复问题;返回的xml数据正常,就是手机上无法看到回复,智商着急,要急死

来源:-

猫熊_

2017-12-23 00:42

public function index(){


    $data=input('get.');
    if(!$data)
    {
        throw new Exception("data无数据");
    }

   // $data=['d','e','rr','4444'];//test

   // $s=implode(',',$data);
    $s='接收到的信息为:';
    foreach ($data  as $key=>$v){
        $s.=$key.':'.$v.';';
    }


    //查看微信传输的数据
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");

    fwrite($myfile, $s.'\n');

    fclose($myfile);

    //加密验证:微信用的sha1加密,加密后返回get中的signature参数
    $timestamp=$data['timestamp'];
    $nonce=$data['nonce'];
    $token='poower';// 与微信服务器配置中放token保持一致
    $signature=$data['signature'];
    $array=array($timestamp,$nonce,$token);
    sort($array);//字典排序
    $str=implode('',$array);
    $strs=sha1($str);
    if($strs==$signature && $data['echostr']) {
        echo $data['echostr'];
        //}elseif(simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'])->MsgType=='event'){//事件判断
    }else{

          $this->responseMsg();
    }


}
public function responseMsg(){
    //1、获取xml数据
    $postArr=$GLOBALS['HTTP_RAW_POST_DATA'];
    //2、处理消息类型,并设置回复类型和内容
    //<xml>
    //<ToUserName>< ![CDATA[toUser] ]></ToUserName>
    //<FromUserName>< ![CDATA[FromUser] ]></FromUserName>
    //<CreateTime>123456789</CreateTime>
    //<MsgType>< ![CDATA[event] ]></MsgType>
    //<Event>< ![CDATA[subscribe] ]></Event>
    //</xml>
    $postObj=simplexml_load_string($postArr);

    $s1='vvvv'.$postObj->FromUserName;
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");

    fwrite($myfile, $s1.'\n');

    fclose($myfile);
    //$postObj->ToUserName;
    //判断该数据包是否是订阅事件的推送
    if(strtolower($postObj->MsgType) == 'event'){
        //如果是关注subscribe事件
        if(strtolower($postObj->Event)=='subscribe'){
            //回复用户消息
            $toUser=$postObj->FromUserName;
            $fromUser=$postObj->ToUserName;
            $time=time();
            $msgType='text';
            $content='欢迎关注我们的微信公众账号'.$fromUser;
            $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);
            $myfile2 = fopen("newfile2.txt", "w") or die("Unable to open file!");

            fwrite($myfile2, $info);

            fclose($myfile2);
             echo $info;

        }
    }
}

两个文件相当于断点,数据均正常,就是最后一步出问题了:newfile2数据:<xml> <ToUserName>< ![CDATA[oDSqD08TZ9VBB5Dk9tPG7pC93IrI] ]></ToUserName> <FromUserName>< ![CDATA[gh_72d52b9a22ac] ]></FromUserName> <CreateTime>1513960368</CreateTime> <MsgType>< ![CDATA[text] ]></MsgType> <Content>< ![CDATA[娆㈣繋鍏虫敞鎴戜滑鐨勫井淇″叕浼楄处鍙穏h_72d52b9a22ac] ]></Content> </xml>


写回答 关注

0回答

还没有人回答问题,可以看看其他问题

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

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

64947 学习 · 371 问题

查看课程

相似问题