TP5 给已关注微信公众号的用户推送消息

  1. 登录微信公众平台
    点击左侧模板消息,在从模版库中添加找到类似的模板点详情后添加。
    在这里插入图片描述
  2. 若没有找到能用的,可自己创建(预计审核7~14天)在这里插入图片描述
    在这里插入图片描述
    ![(https://img-blog.csdnimg.cn/20200527155439965.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzNDA3Mjc5,size_16,color_FFFFFF,t_70)
    在这里插入图片描述
  3. 以下TP5代码

class Message extends Base
{
    public function index()
    {
        $openIdList = $this->getOpenId();//此方法可以获取已关注的openid列表,如需群发用些方法获取

        $openid = 'XXXXXXXXXXXXXX';
        $data = [
            'touser' => $openid,
            'template_id' => 'XXXXXXXXX_Ba5jYrMpm242CHg',//些处为公众平台添加的模板ID
            'url' => $web_url = "http://www.baidu.com",//此处为推送消息“查看详情”的地址
            'topcolor' => "#FF0000",
            'data' => array(
                'first' => array('value' => "公开课内容更新", 'color' => "#fc0101"),//类似副标题
                'keyword1' => array('value' => '平台优势', 'color' => "#173177"),//标题
                'keyword2' => array('value' => '具备互联网科技公司专业研...', 'color' => "#173177"),//内容
            )
        ];
        $get_all_access_token = $this->get_all_access_token();
        $json_data = json_encode($data);//转化成json数组让微信可以接收
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $get_all_access_token;//模板消息请求URL
        $res = $this->curl_post($url, urldecode($json_data));//请求开始
        $res = json_decode($res, true);

        if ($res['errcode'] == 0 && $res['errmsg'] == "ok") {
            return json(['st' => 1, 'msg' => '发送成功']);
        } else {
            return json(['st' => 0, 'msg' => '发送失败']);
        }
    }

    public function get_all_access_token()
    {
        $appid = 'wx66666XXX66666';//公众号appid
        $secret = '2222222XXXXXX222221';//公众号密钥
        $res = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret);
        $resdata = json_decode($res, true);
        $access_token = $resdata['access_token'];
        
        return $access_token;
    }

    /**
     * Describe:获取关注公众号的用户
     * User: yeungz
     * Date: 2020/5/27 14:38
     */
    public function getOpenId()
    {
        $appid = 'wx66666XXX66666';//公众号appid
        $secret = '2222222XXXXXX222221';//公众号密钥
        $res = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret);
        $resdata = json_decode($res, true);
        $access_token = $resdata['access_token'];

        $res1 = file_get_contents("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" . $access_token);
        $resdata1 = json_decode($res1, true);
        $openidList = $resdata1['data']['openid'];

        return $openidList;
    }

    /**
     * 发起GET请求
     * @param string $url 请求地址
     * @param array $data 请求参数
     * @param string $cookiePath 请求所保存的cookie路径
     */
    function curl_get($url, $data = [], $cookiePath = '')
    {
        if (count($data) != 0) {
            $url = get_query_url($url, $data);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }

    /**
     * 发起POST请求
     * @param string $url 请求地址
     * @param string $data 请求参数
     * @param string string $cookiePth
     */
    function curl_post($url, $data = [], $cookiePath = '')
    {
        $ch = curl_init(); // 初始化
        curl_setopt($ch, CURLOPT_URL, $url); // 抓取指定网页
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1); // POST提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // 请求参数
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath); // 连接结束后保存cookie信息的文件
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath); // 包含cookie信息的文件
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 禁用后cURL将终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 检查服务器SSL证书中是否存在一个公用名
        $res = curl_exec($ch); // 执行一个cURL会话
        curl_close($ch); // 关闭一个cURL会话
        return $res;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Oct zz拾月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值