微信公众号之自动回复文字 -- PHP

首先得设置一个 可以接受 微信服务器 发过来 的东西的 无权限  的接收信息的网址

怎么设置自己去官方文档上看看

微信公众号开发文档链接

然后微信 会 发个东西来 验证一下你 的这个网址是否正确

具体验证操作代码如下

/**
     * 微信接入
     */
    function sss() {
        //$this->responseMsg();
//如果相等,验证成功就返回echostr 只需 一次调用
        if ($this->checkSignature()) {
            //返回echostr
            $echostr = $_GET['echostr'];
            if ($echostr) {
                echo $echostr;
            } else {
                echo "Access error.";
            }
        }
        exit;
    }


    //检查标签
    function checkSignature() {
        //先获取到这三个参数
        $signature = $_GET['signature'];
        $nonce = $_GET['nonce'];
        $timestamp = $_GET['timestamp'];
        //把这三个参数存到一个数组里面
        $tmpArr = array($timestamp, $nonce, $this->token);//微信公众号自己设置的值
        //进行字典排序
        sort($tmpArr);
        //把数组中的元素合并成字符串,impode()函数是用来将一个数组合并成字符串的
        $tmpStr = implode($tmpArr);
        //sha1加密,调用sha1函数
        $tmpStr = sha1($tmpStr);
        //判断加密后的字符串是否和signature相等
        if ($tmpStr == $signature) {
            return true;
        }
        return false;
    }

那么验证成功后需要注意的一点

从现在开始,这个发送的网址就已经生效了,需要做的是

把这个验证代码给去掉,去写你的业务逻辑代码

接下来就好处理了,微信公众号上 用户 发的消息,都会经过 微信服务器 转发到你写的这个 方法来

  public function responseMsg() {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //接收微信发来的XML数据
        //extract post data
        if (!empty($postStr)) {
            //解析post来的XML为一个对象$postObj
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName; //请求消息的用户
            $toUsername = $postObj->ToUserName; //"我"的公众号id
            $keyword = trim($postObj->Content); //接受到用户的消息内容
            $Latitude = $postObj->Latitude; //纬度
            $Longitude = $postObj->Longitude; //经度
            $Precision = $postObj->Precision; //地理位置精度
            $Event = $postObj->Event; //事件类型
            $time = time(); //时间戳
            $msgtype = 'text'; //消息类型:文本
            $textTpl = "<xml>
  <ToUserName><![CDATA[%s]]></ToUserName>
  <FromUserName><![CDATA[%s]]></FromUserName>
  <CreateTime>%s</CreateTime>
  <MsgType><![CDATA[%s]]></MsgType>
  <Content><![CDATA[%s]]></Content>
  </xml>";
            /**
             * 地理位置确认
             */
            if ($Event == 'LOCATION') {
                $useraddress = $this->upuser($fromUsername, $Longitude, $Latitude, $Precision);
//                 $this->sendmsg($textTpl, $fromUsername, $toUsername, $time, $msgtype, "您当前位置是:{$useraddress}");
                die;
            }
            //签到设置
            if (strpos($keyword, "签到") !== false ) {
                    $contentStr = "签到成功。";
                }
            }

            if (empty($ary[$keyword]) && empty($contentStr)) {
                $contentStr = M("setting")->where("id = 6 and state = 1")->getField("setval");
                $contentStr = $contentStr ? $contentStr : '欢迎关注乖宝宝儿童摄影。';
            } else {
                $contentStr = $contentStr ? $contentStr : $ary[$keyword];
            }
            $this->sendmsg($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr);
        } else {
            echo "";
        }
    }

/**

*返回消息给 微信服务器,注意格式。。。

*/

 function sendmsg($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr) {
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr);
        echo $resultStr;
    }

猜你喜欢

转载自blog.csdn.net/Purgatory001/article/details/81737662