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 = getMediaId($imgurl);                                    //获取图片消息的media_id
               
// $data=array(
               
//     "touser"=>$fromUsername,
               
//     "msgtype"=>"image",
               
//     "image"=>array("media_id"=>$media_id)
               
// );
                 $content
= '你好,回复1关注公众号,回复2获取官网链接';                
                 $data
=array(
                   
"touser"=>$fromUsername,
                   
"msgtype"=>"text",
                   
"text"=>array("content"=>$content)
               
);
                $json
= json_encode($data,JSON_UNESCAPED_UNICODE);  //php5.4+
                requestAPI
($json);
           
}
           
            elseif
(!empty($postArr['MsgType']) && $postArr['Content'] == "2"){      //用户发送2,回复图文链接
                $fromUsername
= $postArr['FromUserName'];                           //发送者openid
                $data
=array(
                           
"touser"=>$fromUsername,
                           
"msgtype"=>"link",
                           
"link"=>array(                                          //修改下面几项为自己的
                                   
"title"=>'bfw爱编程',    
                                   
"description"=>'bfw爱编程,专业的技术社区',
                                   
"url"=>'https://www.bfw.wiki/',
                                   
"thumb_url"=>'https://repo.bfw.wiki/bfwrepo/icon/60ecf359eb0c5.png',
             
.........完整代码请登录后点击上方下载按钮下载查看

网友评论0