关注微信公众号没有自动回复?

发布于 2022-09-06 00:59:06 字数 1727 浏览 30 评论 0

clipboard.png

<?php

  $nonce=$_GET['nonce'];
  $token='imooc';
  $timestamp=$_GET['timestamp'];
  $echostr=$_GET['echostr'];
  $signature=$_GET['signature'];
//形成数组,然后按字典序排序
$array=array();
$array=array($nonce,$timestamp,$token);
sort($array);
//拼接成字符串,sha1加密,然后与signature进行检验
$str=sha1(implode($array));
if($str==$signature&&$echostr)
{
header('content-type:text');
//第一次接入微信API接口时候验证合法性
echo $echostr;
exit;
}
else
{
responseMsg();
}
 
  function responseMsg()
{
//1.获取到微信推送过来post数据(xml格式)
$postArr=$GLOBALS['HTTP_RAW_POST_DATA'];
//2.处理消息类型,并设置回复类型和内容
$postObj=simplexml_load_string($postArr);
        //判断该数据包是否是订阅de事件推送
        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;
        }
        }

}

已经是接入成功了
难道哪里有写错了?为什么不会自动回复?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我做我的改变 2022-09-13 00:59:06

你好像没有点击启用,还是干什么的,你的代码我放服务器上面跑了下,没问题的。
帮你稍微修改了下

<?php

$nonce = $_GET['nonce'];
$token = 'imooc';
$timestamp = $_GET['timestamp'];
if (isset($_GET['echostr'])) {
    if (bindServerCheck()) {
        echo $_GET['echostr'];
    }
    exit();
}
responseMsg();

 //消息回复
function responseMsg() {
    //1.获取到微信推送过来post数据(xml格式)
    $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
    //2.处理消息类型,并设置回复类型和内容
    $postObj = simplexml_load_string($postArr, 'SimpleXMLElement', LIBXML_NOCDATA);
    //判断该数据包是否是订阅de事件推送
    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;
        }
    }
}

// 开发者模式绑定校验
function bindServerCheck($token) {
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];
    $tmpArr = array(
        $token,
        $timestamp,
        $nonce
    );
    sort($tmpArr);
    $tmpStr = implode($tmpArr);
    $tmpStr = sha1($tmpStr);
    if ($tmpStr == $signature) {
        return true;
    } else {
        return false;
    }
}
五里雾 2022-09-13 00:59:06

感谢楼主和二楼,解决了我的问题!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文