php实现微信小程序客服消息推送接收代码

代码语言:php

所属分类:其他

代码描述:php实现微信小程序客服消息推送接收代码

代码标签: php 微信 小程序 客服 消息 推送 接收 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php
    define("TOKEN", "sdf23424sdfsSDFS!1232");  //填写自己的token,跟小程序后台开发设置-》消息推送填的一样即可,返回形式选json

    if (isset($_GET['echostr'])) {          //校验服务器地址URL
        valid();
    }else{
        responseMsg();
    }
     function valid()
    {
        $echoStr = $_GET["echostr"];
        if(checkSignature()){
            header('content-type:text');
            echo $echoStr;
            exit;
        }else{
            echo $echoStr.'+++'.TOKEN;
            exit;
        }
    }
     function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
    
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
    
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
     function responseMsg()
    {
        $postStr = file_get_contents('php://input');   //此处推荐使用file_get_contents('php://input')获取后台post过来的数据

        if (!empty($postStr) && is_string($postStr)){
        $postArr = json_decode($postStr,true);
            if(!empty($postArr['MsgType']) && $postArr['Content'] == "1"){          //用户发送1,回复公众号二维码
                $fromUsername = $postArr['FromUserName'];                           //发送者openid
                //$imgurl = "/300-300.png";                                           //公众号二维码,相对路径,修改为自己的
              //  $media_id = get.........完整代码请登录后点击上方下载按钮下载查看

网友评论0