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

    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);
    }
     
     
  • 相关阅读:
    MySQL------代码1024,can't get hostname for your address解决方法
    MySQL------存储过程的使用
    MyEclipse------如何添加jspsmartupload.jar,用于文件上传
    JQuery------实现鼠标点击和滑动不同效果
    CSS------如何让div中的div处于右下角
    JQuery------制作div模态框
    CSS------Filter属性的使用方法
    python使用元类
    python __new__()分析
    centos自带python2.6升级到python2.7。并解决yum pip easy_install pip等模块兼容性问题
  • 原文地址:https://www.cnblogs.com/zhangxin-1688/p/13447941.html
Copyright © 2011-2022 走看看