+
95
-

php如何接入微信客服消息实现一对一与客户聊天?

php

php如何接入微信客服消息实现一对一与客户聊天?


网友回复

+
15
-

两种方式:

第一种类似于公众号的消息接受与发送,适合小程序

首先登陆小程序后台

在开发设置中,开启消息推送服务,填写你的服务器url地址,token,选择json数据格式

800_auto

65740ff61ebcc.png

php的微信小程序客服接受消息和推送消息代码

<?php
    define("TOKEN", "213123123123!1232");  //填写自己的token,跟小程序后台填的一样即可

    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:...

点击查看剩余70%

+
15
-

企业微信客服api接入文档

https://developer.work.weixin.qq.com/document/path/94670

我知道答案,我要回答