栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP之 "微信模板消息推送" 的相关代码

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP之 "微信模板消息推送" 的相关代码

温馨提示:
微信模板消息推送,得先去微信端,设置模板配置,会生成相应的消息模板id,这个是一串字符,需连同参数数据,传给微信端,切记!~
第一部分:主体部分代码,负责业务模块,准备微信端所需参数
public function order_complete_wx_notice($orderid)
{
    //$this->write_log(['order_cmp'=>1,'$orderid'=>$orderid]);
    if($orderid){
        // 通过orderid获取模板展示的相关信息
        $order_info = $this->get_template_info($orderid, 3);
        //$this->write_log(['order_cmp'=>2,'order_info'=>$order_info]);
        if($order_info){
            // 模板数据
            $request = [];
            $request['touser'] = $order_info['openid'];
            $request['template_id'] = '84Muc5Er_gyddmU1sdfdXSssssdAvW93kI'; // 订单完成消息模板id
            $request['page'] = 'pages/near/index'; // 点击模板卡片跳转页面
            $request['form_id'] = $order_info['prepay_id']; // 本次支付的id
            $request['data'] = [
                'keyword1'=>['value'=>$order_info['course_name']], // 课程名称
                'keyword2'=>['value'=>$order_info['order_complete_time']], // 完成时间
                'keyword3'=>['value'=>$order_info['gym_name']], // 门店房名
                'keyword4'=>['value'=>$order_info['address']], // 门店地址
                'keyword5'=>['value'=>'已完成'], // 订单状态
                'keyword6'=>['value'=>'400-010-88888'], // 客服电话
                'keyword7'=>['value'=>'您的本次订单已完成,欢迎致电客服,提出宝贵意见哦!'], // 温馨提示
            ];
            //$request['emphasis_keyword'] = 'keyword5.DATA'; // 模板放大关键词
            //$this->write_log(['order_cmp'=>3,'request'=>$request]);
            // 发送数据
            $this->wx_show_template($request);
        }
    }
}


public function get_template_info($id, $type)
{
    if($id && $type){
        $this->load->dao('order_dao');
        return $this->order_dao->get_template_info($id, $type);
    }
}
第二部分:负责将准备好的数据传输到微信端,微信再给用户微信推送模板消息
public function wx_show_template($request)
{
    // 获取 redis中的 redis_access_token 值
    $access_token = $this->get_redis_access_token();
    if(!$access_token){
        // 从微信端获取
        $access_token = $this->get_access_token();
        // 获取存入redis
        $this->load->library('dbredis');
        $this->dbredis->set('redis_access_token', $access_token);
    }
    if(!$access_token){
        exit('access_token有误!');
    }
    // 发送地址
    $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;
    // 请求微信,微信端显示模板
    $this->curl_post_weixin($url, $request);
}


public function curl_post_weixin($url, $data)
{
    if($url && count($data)){
        $headers = ['Content-Type:application/json'];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 关键点
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_exec($ch);
        curl_close($ch);
    }
}


public function get_access_token()
{
    $appConfig = [
        'app_id' => 'wxsdd9asdfghe5efc',
        'secret' => 'ec1879wiujhyytbdt786ddb7d29106'
    ];
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appConfig['app_id'].'&secret='.$appConfig['secret'];
    $ch = curl_init(); // 创建句柄
    curl_setopt($ch, CURLOPT_URL, $url); // 通过url获取数据
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 跳过证书验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是
    $output = json_decode(curl_exec($ch));
    $access_token = $output->access_token;
    curl_close($ch);
    return $access_token;
}
最后总结:
微信模板消息推送,关键的点,是先去微信端进行配置生成相应的模板,会生成模板id,然后,就是准备参数,通过curl函数对数据传送,微信端收到消息后,会根据openID,将消息推送给对应的用户的(手机)微信端!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/268689.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号