欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微信公众号模板消息接口

程序员文章站 2023-12-28 14:59:34
...

1. 准备工作, 申请账号, 添加消息模板

2.主要代码如下:

   send.php

 

<?php
/**
 * Created by PhpStorm.
 * User: a
 * Date: 2019-05-13
 * Time: 14:39
 */

namespace app\index\controller;

use think\Controller;
use RedisPackage;


class Send extends Controller
{
    protected $appid = 'AAAAA'; //appid
    protected $appsecret = 'BBBB';  //AppSecret

    public function send_msg(){
        $data = array();
        $data['touser'] = 'CCCCCC';//接受者的openid
        $data['template_id'] = 'DDDDDD';//消息模板的template_id
        $data['appid'] = 'EEEEE';//小程序appid
        $data['data']['first']['value'] = "您好!";
        $data['data']['number']['value'] = "您收藏的商品【'双面呢大衣'】买三送一";
        $data['data']['name']['value'] = "即将结束,请及时下单";
        $data['data']['remark']['value'] = "距离本场拍卖结束还有30分钟";

        $data1 = json_encode($data);
        $res = $this->send_template_message($data1);
        return $res;
    }

    protected function send_template_message($data=''){
        if(empty($data)){
            echo 'null';exit;
        }

        $access_token = $this->getToken();

        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
        $res = $this->https_request($url,$data);
        $data = json_decode($res,true);

        if(in_array($data[ 'errcode' ],[40001,40014,41001,42001])){
            $access_token = $this->getAccessToken();
            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
            $res = $this->https_request($url,$data);
            $data = json_decode($res,true);
        }
        return $data;
    }

    /*
    * 获取accesstoken
    * 调用接口时,提前将服务器IP地址添加到IP白名单中,否则报错40164
    */
    protected function getToken(){
        $redis = RedisPackage::getInstance();
        $access_token = $redis->get('token_redis');

        if(!$access_token) {
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->appsecret;

            $output = $this->https_request($url);

            $jsoninfo = json_decode($output, true);

            if(!array_key_exists("access_token", $jsoninfo)) {
                return $jsoninfo;
            }
            $access_token = $jsoninfo["access_token"];

            $redis->setex('token_redis', 7000, $access_token);

            return $access_token;
        } else {
            return $access_token;
        }
    }


    protected function getAccessToken()
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->appsecret;

        $output = $this->https_request($url);

        $jsoninfo = json_decode($output, true);

        if(!array_key_exists("access_token", $jsoninfo)) {
            return $jsoninfo;
        }
        $access_token = $jsoninfo["access_token"];

        $redis = RedisPackage::getInstance();

        $redis->del('token_redis');

        $redis->setex('token_redis', 7000, $access_token);

        return $access_token;
    }

    /*
    * $url 接口API
    * $data json数据
    */
    protected 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;
    }
}

 

微信全局返回码说明文档:

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

微信模板消息接口文档:

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

上一篇:

下一篇: