zoukankan      html  css  js  c++  java
  • 微信商城用户购买商品后,推送模板消息给用户

    例子:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277

      使用效果

      

    1 设置所属行业

    2 获取设置的行业信息

    3 获得模板ID

    4 获取模板列表

    5 删除模板

    6 发送模板消息

    7 事件推送

    以上是推送模板消息,官方推荐步骤,本人再此处不用,第四部之前都是后台操作的,所以在此处,已经知道模板id了,直接进行第六步发送消息

    第一步:首先获取appid。appsecret信息,然后通过微信提供的api地址进行获取accesstoken

    第二布:通过sendMessage方法发送消息,完成

    $appid = $GLOBALS['cfg_weixi_appkey'];
    $appsecret = $GLOBALS['cfg_weixi_appsecret'];
    if ($appid && $appsecret) {
    $atoken = Common::get_token($appid, $appsecret);//获取ACCESS_TOKEN
    }
    $member = Common::session('member');//获取用户id
    if (!empty($member)) {
    $notuse = DB::select('wechatcode', 'nickname')->from('member')
    ->where('mid', '=', $member['mid'])
    ->execute()
    ->current();//获取用户表中wechatcode字段的openid信息
    $oppenid = $notuse['wechatcode'];
    $proname = '商品名称';
    $price = '588';
    $report = '啊飒飒及时';
    $rest = Common::sendMessage($atoken, $oppenid, $proname, $price, $report);//通过这个方法发送模板信息
    }

    var_dump($rest);die;

    以下是,调用的两个方法:

    /**
    * [获得全局access_token]
    * @copyright [copyright]
    * @author docblokr 2018-07-18
    * @version [version]
    * @param [type] $appid [微信appid]
    * @param [type] $appsecret [微信的appsecret]
    * @return [type] [description]
    */
    public static function get_token($appid, $appsecret)
    {
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
    $tmpInfo = curl_exec($curl); //返回api的json对象
    //关闭URL请求
    curl_close($curl);
    $arr = json_decode($tmpInfo, true); //将结果转为数组
    return $arr['access_token'];//最终获取的accesstoken
    }

    /**
    * [推送模板信息]
    * @copyright [参数]
    * @author docblokr 2018-07-18
    * @version [version]
    * @param [type] $token [ACCESS_TOKEN]
    * @param [type] $openid [发送给谁的openid]
    * @param [type] $proname [购买的商品名称]
    * @param [type] $price [价格]
    * @param [type] $report [自定义发送的内容]
    * @return [type] [description]
    */
    public static function sendMessage($token, $openid, $proname, $price, $report)
    {
    $urlm = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $token; //发送模板信息请求地址
    //发送的模板信息(微信要求json格式,这里为数组(方便添加变量)格式,然后转为json)
    $post_data = array(
      "touser" => $openid, //推送给谁,openid
      "template_id" => "ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY", //微信后台的模板信息id
      "url" => "http://www.baidu.com", //模板跳转链接
      "data" => array(
      "first" => array(
        "value" => "恭喜你购买成功!",
        "color" => "#173177"
       ),
      "proname" => array(
        "value" => $proname, //传的变量
        "color" => "#173177"
      ),
      "price" => array(
        "value" => $price,
        "color" => "#173177"
      ),
      "report" => array(
        "value" => $report,
        "color" => "#173177"
      ),
      "reporttime" => array(
        "value" => date('Y-m-d H:i:s'),
        "color" => "#173177"
      ),
      "remark" => array(
        "value" => "欢迎再次购买!",
        "color" => "#173177"
      ),
        )
    );
    //将上面的数组数据转为json格式
    $post_data = json_encode($post_data);
    /*发送模板消息
    http请求方式: POST
    https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
    */
    //发送数据,post方式发送模板消息
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $urlm);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($post_data)) {
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
    }

  • 相关阅读:
    设计模式---单例模式
    Linux介绍
    集合---Map
    集合---Collection
    JDK1.7中HashMap底层实现原理(转)
    算法面试题-用单向链表表示十进制整数,求两个正整数之和。1234+34=1268
    idea 常用插件
    ehcache配置log4j日志,或与spirng cache整合用注解形式,打印注解日志
    spring cache @CacheEvict 清除多个key
    tomcat源码及其下载版本
  • 原文地址:https://www.cnblogs.com/yszr/p/9330299.html
Copyright © 2011-2022 走看看