微信公众号之模板推送

工作需要写的一个小的推送功能
准备好appid和密钥 并且维信诺后台配置了相应的域名(且备案)

<?php
namespace app\api\controller;
class Wxpush
{

     public function wxLogin()
    {
        $AppID     = 'wxb722d21';
        $redirect_uri = urlencode('https://t.fblfz.com/api/wxpush/getUserInfo');
        //跳转微信回调到redirect_uri获取code
        $url1 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$AppID&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
        return redirect($url1);//参数scop=snsapi_base只获取openid
    }
    public function getUserInfo()//静默授权
        {
            $code      = $_GET["code"];
            $AppID     = 'wxb727716562d21';
            $Appsecret = '522c94a5c2d';
            $url       = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$AppID&secret=$Appsecret&code=$code&grant_type=authorization_code";
            $res       = $this->sendRequest($url);
            $access_token = $res["access_token"];
            $openId       = $res['openid'];
            echo '联系管理员拿取Openid';
            $stream = fopen("logs.txt", "a");fwrite($stream, "\n".$openId);
        }
    public function sendurl($url, $data) //菜单栏发送
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }
    public function access($user,$formurls,$phone,$txt)
    {

        $access_token = $this->getacc();//拿取acc
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
         $data = array(
            "touser"        =>  'oOEJw1TVSRarpvsFNPJh7ZvXDBz8',        //openid
            "template_id"   =>  'ac05DpJtls-kOopcm1sMberZRRWpSHn-F0jQAUgc_jY',             //模板id
            "url"           =>  '用户点击信息跳转地址',
            "data"          =>  array(
                                    'first' => array(
                                        'value'=>'尊敬的客户,您的留言有了新的消息',
                                        'color'=>'red'
                                    ),
                                    'keyword1'=>array(
                                        'value'=>'留言人:'.$user,
                                        'color'=>''
                                    ),
                                    'keyword2'=>array(
                                        'value'=>'网址:'.$formurls,
                                        'color'=>''
                                    ),
                                    'keyword3'=>array(
                                        'value'=>'留言手机号:'.$phone,
                                        'color'=>''
                                    ),
                                    'keyword4'=>array(
                                        'value'=>'留言内容:'.$txt,
                                        'color'=>''
                                    ),
                                    'keyword5'=>array(
                                        'value'=>'留言时间:'.date('Y-m-d H:i:s', time()),
                                        'color'=>''
                                    ),
                                    'remark'    => array(
                                        'value'=>'新用户留言!!!',
                                        'color'=>''
                                    ),
                                ),        //模板数据
        );
        $this->sendurl($url,json_encode($data));//发送请求
    }

    public function getacc(){
                $AppID = 'wxb7279f92d21';
                $Appsecret = '54ce3686c44a5c2d';
                $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$AppID."&secret=".$Appsecret;
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

                if (!empty($data)){
                    curl_setopt($ch, CURLOPT_POST, TRUE);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                }
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $output = curl_exec($ch);
                curl_close($ch);
                $access = json_decode($output,true);
              return $access['access_token'];
    } 

        public function sendRequest($url)//静默授权时使用
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            return json_decode($output, true);
        }
}

去维信诺模板库找对应的模板id
opendi是接收人

为您推荐