博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小程序客服消息接口,接入及消息接收
阅读量:5291 次
发布时间:2019-06-14

本文共 6420 字,大约阅读时间需要 21 分钟。

   public function index(){     //判断是否为认证         if (isset($_GET['echostr'])) {        //如果认证去验证            $this->valid();        }else{        //否则接收客户发送消息            $this->responseMsg();        }            }  //验证前置方法    public function valid()    {        $echoStr = $_GET["echostr"];        if($this->checkSignature()){            header('content-type:text');            echo $echoStr;            exit;        }else{            echo $echoStr.'+++'.TOKEN;            exit;        }    }  //签名校验    private function checkSignature()    {     //微信加密签名        $signature = $_GET["signature"];     //时间戳        $timestamp = $_GET["timestamp"];     //随机数        $nonce = $_GET["nonce"];      //服务端配置的TOKEN        $token = '自己配置的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;        }    }    public function responseMsg()    {    //接收来自小程序的客户消息JSON        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];        if (!empty($postStr) && is_string($postStr)){            //禁止引用外部xml实体            //libxml_disable_entity_loader(true);                        //$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);            $postArr = json_decode($postStr,true);            if(!empty($postArr['MsgType']) && $postArr['MsgType'] == 'text'){   //文本消息                $fromUsername = $postArr['FromUserName'];   //发送者openid                $toUserName = $postArr['ToUserName'];       //小程序id                $textTpl = array(                    "ToUserName"=>$fromUsername,                    "FromUserName"=>$toUserName,                    "CreateTime"=>time(),                    "MsgType"=>"transfer_customer_service",                );                exit(json_encode($textTpl));            }elseif(!empty($postArr['MsgType']) && $postArr['MsgType'] == 'image'){ //图文消息                $fromUsername = $postArr['FromUserName'];   //发送者openid                $toUserName = $postArr['ToUserName'];       //小程序id                $textTpl = array(                    "ToUserName"=>$fromUsername,                    "FromUserName"=>$toUserName,                    "CreateTime"=>time(),                    "MsgType"=>"transfer_customer_service",                );                exit(json_encode($textTpl));            }elseif($postArr['MsgType'] == 'event' && $postArr['Event']=='user_enter_tempsession'){ //进入客服动作                $fromUsername = $postArr['FromUserName'];   //发送者openid                $content = '您好,有什么能帮助你?';                $data=array(                    "touser"=>$fromUsername,                    "msgtype"=>"text",                    "text"=>array("content"=>$content)                );                $json = json_encode($data,JSON_UNESCAPED_UNICODE);  //php5.4+                                $access_token = $this->get_accessToken();                /*                  * POST发送https请求客服接口api                 */                $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;                //以'json'格式发送post的https请求                $curl = curl_init();                curl_setopt($curl, CURLOPT_URL, $url);                curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);                if (!empty($json)){                    curl_setopt($curl, CURLOPT_POSTFIELDS,$json);                }                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);                //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );                $output = curl_exec($curl);                if (curl_errno($curl)) {                    echo 'Errno'.curl_error($curl);//捕抓异常                }                curl_close($curl);                if($output == 0){                    echo 'success';exit;                }                            }else{                exit('aaa');            }        }else{            echo "";            exit;        }    }    /* 调用微信api,获取access_token,有效期7200s -xzz0704 */    public function get_accessToken(){        /* 在有效期,直接返回access_token */        if(S('access_token')){            return S('access_token');        }        /* 不在有效期,重新发送请求,获取access_token */        else{            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx6056****&secret=30e46f3ef07b****';            $result = curl_get_https($url);            $res = json_decode($result,true);   //json字符串转数组                if($res){                S('access_token',$res['access_token'],7100);                return S('access_token');            }else{                return 'api return error';            }        }    }    public function message(){        $code = $_GET['code'];        $appid='wx6da1e8575401a942';        $appSecret='e64fa3f371bb91bfc2b6c28f008f3174';        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$appSecret.'&js_code='.$code.'&grant_type=authorization_code';                $res = $this->http_request($url);        $res1 = json_decode($res);        $access_token = $this->oauth2_access_token($code);        $this->ajaxReturn(array('data'=>$res1,'access_token'=>$access_token));    }    public function oauth2_access_token($code)     {         $appid='wx6da1e8575401a942';        $appSecret='e64fa3f371bb91bfc2b6c28f008f3174';         $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code";         $res = $this->http_request($url);         return json_decode($res, true);     }    protected function http_request($url, $data = null)     {         $curl = curl_init();         curl_setopt($curl, CURLOPT_URL, $url);         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);         if (!empty($data)){             curl_setopt($curl, CURLOPT_POST, 1);             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);         }         curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);         $output = curl_exec($curl);         curl_close($curl);         return $output;     }

 

转载于:https://www.cnblogs.com/objects/p/7889088.html

你可能感兴趣的文章
图像状态资源的介绍~~以button按钮为例
查看>>
【转】eclipse技巧2
查看>>
Vue.js组件之同级之间的通信
查看>>
javascript中的面向对象(一)
查看>>
Android计算器界面 TableLayout
查看>>
【软件工程】敏捷开发方法的总结
查看>>
路由器原理及作用以及交换机
查看>>
主流PC浏览器调研
查看>>
Linux权限管理 - 基本权限
查看>>
C语言初学 简单计算器计算加减乘除程序
查看>>
smali语法小结
查看>>
[python]-类的继承
查看>>
pkg-config 设置
查看>>
选择之后,不能再选择。分配之后,不能再分配
查看>>
修复Kaos的中文显示
查看>>
自学it18大数据笔记-第三阶段Spark-day03——会持续更新……
查看>>
HBase总结
查看>>
xcode 快捷键
查看>>
STM32 CubeMX 中如何查看系统时钟
查看>>
C# 操作excel
查看>>