• 微信公众号(小程序)利用客服接口主动给用户发送消息的方法


    1、用户发送信息
    2、点击自定义菜单(仅有点击推事件、扫码推事件、扫码推事件且弹出“消息接收中”提示框这3种菜单类型是会触发客服接口的)
    3、关注公众号
    4、扫描二维码
    5、支付成功
    6、用户维权

    公共方法

    function https_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, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }


    获取token

    将token存放在本地,获取token先判断token是否过期,过期则重新接口获取,否则直接取本地的

    function getToken()
    {
        global $APPID;
        global $APPSECRET;
        $token_file = dirname(__FILE__).'/data/token.txt';
        if(!file_exists($token_file) || ((time() - filemtime($token_file)) > 7000)){
            $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;
    
            $json=file_get_contents($TOKEN_URL);
            $result=json_decode($json);
    
            $ACC_TOKEN=$result->access_token;
            file_put_contents($token_file,$ACC_TOKEN);
        }else{
            $ACC_TOKEN = file_get_contents($token_file);
        }
        return $ACC_TOKEN;
    }

    添加客服

    请先在微信公众平台后台开通微信客服

    function addkf()
    {
        $url = 'https://api.weixin.qq.com/customservice/kfaccount/add?access_token='.getToken();
    
        $data = '{
             "kf_account" : "system@system",
             "nickname" : "客服1",
             "password" : "systemsystem",
        }';
        echo https_request($url,$data);
    }
     

    发送消息

    function sendmsg(){
        $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.getToken();
        $data = '{
            "touser":"接收用户的openid",
            "msgtype":"text",
            "text":
            {
                 "content":"Hello World"
            }
        }';
        echo https_request($url,$data);
    }
     
     
  • 相关阅读:
    Icinga使用总结
    Linux入门:usermod
    JMeter入门(01)概念和样例
    英语日常词汇:living-room、dining-room vs dining hall
    安装 docker-compose
    CentOS7下安装python-pip
    JMeter入门(03)多台JMeter联合测试
    jmeter入门(02)测试报告各项指标含义
    istio入门(01)istio的优势在哪里?
    Docker学习笔记
  • 原文地址:https://www.cnblogs.com/zhangxin-1688/p/13447941.html
Copyright © 2020-2023  润新知