微信 推送模板消息

直接上代码

代码直接复制  修改其中的 appid,appsecret,用户openid,消息模板ID  直接执行就可以

<?php
// 字符编码
header("Content-Type:text/html; charset=utf-8");

// 微信接口类
class WeChat{
    private static $appid;
    private static $appsecret;

    function __construct(){
        self::$appid = '';      // 开发者ID(AppID)
        self::$appsecret = '';  // 开发者密码(AppSecret)
    }

    // 微信授权地址
    public static function getAuthorizeUrl($url){
        $url_link = urlencode($url);
        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . self::$appid . "&redirect_uri={$url_link}&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
    }

    // 获取TOKEN
    public static function getToken(){
        $urla = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . self::$appid . "&secret=" . self::$appsecret;
        $outputa = self::curlGet($urla);
        $result = json_decode($outputa, true);
        return $result['access_token'];
    }

    /**
     * getUserInfo 获取用户信息
     * @param  string $code         微信授权code
     * @param  string $weiwei_token Token
     * @return array
     */
    public static function getUserInfo($code, $weiwei_token){
        $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . self::$appid . "&secret=" . self::$appsecret . "&code={$code}&grant_type=authorization_code";
        $access_token_json = self::curlGet($access_token_url);
        $access_token_array = json_decode($access_token_json, true);
        $openid = $access_token_array['openid'];
        $new_access_token = $weiwei_token;

        //全局access token获得用户基本信息
        $userinfo_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$new_access_token}&openid={$openid}";
        $userinfo_json = self::curlGet($userinfo_url);
        $userinfo_array = json_decode($userinfo_json, true);
        return $userinfo_array;
    }

    /**
     * pushMessage 发送自定义的模板消息
     * @param  array  $data          模板数据
        $data = [
            'openid' => '', 用户openid
            'url' => '', 跳转链接
            'template_id' => '', 模板id
            'data' => [ // 消息模板数据
                'first'    => ['value' => urlencode('张'),'color' => "#743A3A"],
                'keyword1' => ['value' => urlencode('男'),'color'=>'blue'],
                'keyword2' => ['value' => urlencode('1996-10-23'),'color' => 'blue'],
                'remark'   => ['value' => urlencode('我的模板'),'color' => '#743A3A']
            ]
        ];
     * @param  string $topcolor 模板内容字体颜色,不填默认为黑色
     * @return array
     */
    public static function pushMessage($data = [],$topcolor = '#0000'){
        $template = [
            'touser'      => $data['openid'],
            'template_id' => $data['template_id'],
            'url'         => $data['url_link'],
            'topcolor'    => $topcolor,
            'data'        => $data['data']
        ];
        $json_template = json_encode($template);
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . self::getToken();
        $result = self::curlPost($url, urldecode($json_template));
        $resultData = json_decode($result, true);
        return $resultData;
    /*返回码
	*array(3) {
	*  ["errcode"]=>
	*  int(0)
	*  ["errmsg"]=>
	*  string(2) "ok"
	*  ["msgid"]=>
	*  float(4.264863832106E+17)
	*}
	*其中 errcode=0 和 errmsg=ok  均代表成功
    */
    }

    /**
     * addLog 日志记录
     * @param string $log_content 日志内容
     */
    public static function addLog($log_content = ''){
        $data = "";
        $data .= "DATE: [ " . date('Y-m-d H:i:s') . " ]\r\n";
        $data .= "INFO: " . $log_content . "\r\n\r\n";
        file_put_contents('/wechat.log', $data, FILE_APPEND);
    }

    /**
     * 发送get请求
     * @param string $url 链接
     * @return bool|mixed
     */
    private static function curlGet($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        if(curl_errno($curl)){
            return 'ERROR ' . curl_error($curl);
        }
        curl_close($curl);
        return $output;
    }

    /**
     * 发送post请求
     * @param string $url 链接
     * @param string $data 数据
     * @return bool|mixed
     */
    private static function curlPost($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;
    }
}

/**
 * get_page_url 获取完整URL
 * @return url
 */
function get_page_url($type = 0){
    $pageURL = 'http';
    if($_SERVER["HTTPS"] == 'on'){
        $pageURL .= 's';
    }
    $pageURL .= '://';
    if($type == 0){
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }else{
        $pageURL .= $_SERVER["SERVER_NAME"];
    }
    return $pageURL;
}

// 获取用户openid

// 微信接口类
$WeChat = new WeChat();
if(empty($_GET['code']) || !isset($_GET['code'])){
    // 通过授权获取code
    $url = get_page_url();
    $authorize_url = $WeChat->getAuthorizeUrl($url);
    header("Location:{$authorize_url}"); // 重定向浏览器
    exit();
}else{
    // 获取微信用户信息
    $code = $_GET['code'];
    $weiwei_token = $WeChat->getToken(); // 获取微信token
    $user_info = $WeChat->getUserInfo($code, $weiwei_token);
    $openid = $user_info['openid'];
    # 公众号消息推送
    $WeChat::pushMessage([
        'openid' => $openid, // 用户openid
        'access_token' => $weiwei_token,
        'template_id' => "ONZapeZi5OzxHym7IaZw7q4eJHEV4L6lzdQrEIWBs60", // 填写你自己的消息模板ID
        'data' => [ // 模板消息内容,根据模板详情进行设置
            'first'    => ['value' => urlencode("尊敬的某某某先生,您好,您本期还款已成功扣收。"),'color' => "#743A3A"],
            'keyword1' => ['value' => urlencode("2476.00元"),'color'=>'blue'],
            'keyword2' => ['value' => urlencode("13期"),'color'=>'blue'],
            'keyword3' => ['value' => urlencode("15636.56元"),'color' => 'green'],
            'keyword4' => ['value' => urlencode("6789.23元"),'color' => 'green'],
            'remark'   => ['value' => urlencode("更多贷款详情,请点击页面进行实时查询。"),'color' => '#743A3A']
        ],
        'url_link' => 'https://www.cnblogs.com/' // 消息跳转链接
    ]);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值