微信公众号发送模版消息
侧边栏壁纸
博主昵称
404

  • 累计撰写 17 篇文章
  • 累计收到 0 条评论
PHP

微信公众号发送模版消息

404
404
2022-07-05 / 0 评论 / 16 阅读 / 正在检测是否收录...
微信公众号用户的OpenId获取方式之一
通过微信接口
1.1)获取网页授权code(分为静默授权,非静默授权(需用户确认))
1.2) 通过授权code获取用户信息(用户信息包含两种:1)只取OpenId,2)取用户相关头像、昵称。openId等)

通过公众号用户获取:
直接在登陆公众号-找到管理--> 用户信息-找到已关注用户列表-用F12打开
打开调试模式,选中某个用户-找到label下的value值,这就是用户的openId
<?php
class Index{
 
  static function getToken(){
   
        $appid ='appid';
        $secret = 'secret';
        $urla = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .$appid . "&secret=" . $secret;
        $outputa = self::curlGet($urla);
        $result = json_decode($outputa, true);
        //  var_dump($result);
        return $result['access_token'];
    }
 
    /**
     * 发送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;
    }
 

     static function pushMessage($data = [],$topcolor = '#0000'){
        $template = [
            'touser'      => $data['openid'],
            'template_id' => $data['template_id'],
            'url'         => $data['url'],
            '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;
    }
 

 
    public function sendMsg(){
        $weiwei_token=self::getToken();
        
        // echo $weiwei_token;die();
        $data=[
            'openid' => 'o-用户openid', // 用户openid
            'access_token' => $weiwei_token,
            'template_id' => "填写你自己的消息模板ID", // 填写你自己的消息模板ID
            'data' => [ // 模板消息内容,根据模板详情进行设置
                'first'    => ['value' => urlencode("尊敬的某某某先生,您好,您太帅了"),'color' => "#743A3A"],
                'keyword1' => ['value' => urlencode("2019必胜猪"),'color'=>'blue'],
                'keyword2' => ['value' => urlencode("2019必胜猪"),'color'=>'blue'],
                'keyword3' => ['value' => urlencode("2019-09-06"),'color' => 'green'],
                'keyword4'   => ['value' => urlencode("请收下我的膝盖"),'color' => '#743A3A']
            ],
            'remark'=>'上海xxx'
        ];
        echo "<pre>";
        var_dump(self::pushMessage($data));
        echo "</pre>";
        echo "<hr>";
        echo "<pre>";
        var_dump($data);
        echo "</pre>";
 
    }
    

}
    $Index =  new Index();
    $Index->sendMsg();

   var_dump($Index->sendMsg()); 
0

评论 (0)

取消