微信公众号开发 tp5写发送模板消息

1.发送模板消息方法,将用户openid,模板id等信息封装成数组提交,openid这里我用的是微信静默授权,因为用到好几个公众号的东西

public function sendinfo($out_trade_no){

        //订单信息

        

        $orderinfo = Db::name('ns_order')->where('out_trade_no',$out_trade_no)->field('order_id,finish_time,pay_money,user_name,order_no,create_time,shop_id,buyer_id')->find();

        $shop_name = Db::name('ns_shop')->where('shop_id',$orderinfo['shop_id'])->value('shop_name');

        $url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];

        $openid = Db::name('sys_user')->where('uid',$orderinfo['buyer_id'])->value('ts_openid');

        

        //提交成功,触发信息推送

        $data=[

            'touser'=> $openid,

            'template_id'=>'Nwdr12G416r_JAPY40QrWjP_gQK8iSds0dy8ns35amg',

            'url'=>$web_url = "http://".$_SERVER['SERVER_NAME'],//用户收到消息后点击调整的url,可自定义

            'topcolor'=>"#FF0000",

            'data'=>array(

                'first' => "新的订单",

                'keyword1'=>array('value'=>$shop_name,'color'=>"#fc0101"),

                'keyword2'=>array('value'=>$orderinfo['pay_money'],'color'=>"#173177"), 

                'keyword3'=>array('value'=>'微信支付','color'=>"#173177"), 

                'keyword4'=>array('value'=>$orderinfo['order_no'],'color'=>"#173177"), 

                'keyword5'=>array('value'=>$orderinfo['finish_time'],'color'=>"#173177"), 

                'remark'=>array('value'=>"进入商城",'color'=>"#173177"),

            )

        ];

        

        $get_access_token = $this->get_access_token();

        $json_data=json_encode($data);//转化成json数组让微信可以接收

        

        $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$get_access_token;//模板消息请求URL

        $res=$this->https_request($url,urldecode($json_data));//请求开始

        $res=json_decode($res,true);

        if($res['errcode']==0 && $res['errcode']=="ok"){            

            return 1;

        }else{

            return 0;

        }

    }

2.curl提交方法

public 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;

    }

3.获取access_token

/**

     * 获取access_token

     *

     * @return void

     */

     public function get_access_token(){

 

        $appID = "wxeb29d40db7406661";

        $appsecret="ecf0ebcf9ac6d6a37f18875b24dabec4";

        $get_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appID."&secret=".$appsecret;

        

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $get_url);

        curl_setopt($curl, CURLOPT_HEADER, 0);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $data = curl_exec($curl);

        curl_close($curl);

 

        $token = json_decode($data);

        return($token->access_token);

    }

4.获取openid

/**

      *  通过code换取openid

    */

    public function getUserOpenId(){

        if (!isset($_GET['code']))

        {

            $codeUrl = $this->getWechatAuthCode();

            Header("Location: $codeUrl");

            die;

        }else{

            $code = $_GET['code'];

            $this->code = $code;

 

            // 请求openid

            $appid = "wxeb29d40db7406661";

            $secret = "ecf0ebcf9ac6d6a37f18875b24dabec4";

            $code = $this->code;

            $userOpenIdUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";

            // $data = httpRequest($url,'GET');

            $curl = curl_init();

            curl_setopt($curl, CURLOPT_URL, $userOpenIdUrl);

            curl_setopt($curl, CURLOPT_HEADER, 0);

            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

            $data = curl_exec($curl);

 

            curl_close($curl);

            //取出openid

            $arr = json_decode($data);

            return $arr->openid;

        }

    }

5.获取openid所需的code

/**

    *  获取code

    */

    public function getWechatAuthCode(){

        // 获取来源地址

        $url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];

        $wechatAuthCodeUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?';

        // 获取code

        $urlObj["appid"] = 'wxeb29d40db7406661';

        $urlObj["redirect_uri"] = "$url";

        $urlObj["response_type"] = "code";

        $urlObj["scope"] = "snsapi_base";

        $urlObj["state"] = "STATE"."#wechat_redirect";

        $bizString = $this->formatBizQueryParaMap($urlObj, false);

        $codeUrl =  $wechatAuthCodeUrl.$bizString;

 

        return $codeUrl;

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值