微信公众号测试号配置+模版消息开发

【1】踩了一上午的坑 接口配置一直不成功,直到方法换成index
在这里插入图片描述

//验证
    public function index()
    {
        echo input('echostr');
    }

这儿是一个神奇的地方,之前写的代码不成功,换成这个之久就成功了 ,比较神奇

之前的代码

    //验证
    public function checkSignature()
    {

        $signature = input('signature');
        $timestamp = input('timestamp');
        $nonce = input('nonce');

        $token = 'cJ8CQE5ZQ5QgjOV5g8C9xUuOqoZA4Nqj';
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }


    }

开始开发模版消息

 //【会员充值】发送模版消息
    public function set_msg($out_trade_no,$total_fee){

        $uri ='https://api.weixin.qq.com/cgi-bin/message/template/send';
        $access_token = $this->getAccess_token();;
        $uri = $uri.'?access_token='.$access_token;

        $data=  array('touser'=>'oXQ9Gt3j79F6m3kSKZteEiICkQLw',   //发给谁
            'template_id'=>'wRaL7SsmjNAWocMjbo-bGo4Pdmy5vNZpTEbwSz19M5E',   //消息模板id
            'url'=>'#',     //这个是你发送了模板消息之后,当用户点击时跳转的连接
            'topcolor'=>"#FF0000",   //颜色
            'miniprogram' => '',
            'data'=>array(

                'first'=>array(
                    'value'=>'会员充值',
                    'color'=>'#173177'
                ),
                'keyword1'=>array(
                    'value'=>date('Y-m-d H:i:s',time()),
                    'color'=>'#173177'
                ),
                'keyword2'=>array(
                    'value'=>$total_fee,
                    'color'=>'#173177'
                ),
                'keyword3'=>array(
                    'value'=>$out_trade_no,
                    'color'=>'#173177'
                ),
                'remark'=>array(
                    'value'=>'您的充值金额已到账,请注意查看',
                    'color'=>'#173177'
                )
            )
        );
        $res_data = $this->sendUrl($uri,$data);
        $res_data = json_decode($res_data, true);
        if ($res_data['errcode'] != 0) {
            return false;
        }
        return true;
    }


    //获取Access_token
    public function getAccess_token(){
        $appid='wxdffdacdd4ee5795b';
        $appsecret='43306c632356d60d2c407a4108dad57d';
        $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret.'';
        $data=$this->sendUrl($url);
        $res=json_decode($data,true);
        return $res['access_token'];
    }


    //发送请求
    function sendUrl($url = '', $param = [] ,$contentType = 'json'){
        $ch = curl_init();
        // 请求地址
        curl_setopt($ch, CURLOPT_URL, $url);
        // 请求参数类型
        $param = $contentType == 'json' ? urldecode(json_encode($param)) : http_build_query($param);
        // 关闭https验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        // post提交
        if($param){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        }
        // 返回的数据是否自动显示
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 执行并接收响应结果
        $output = curl_exec($ch);
        // 关闭curl
        curl_close($ch);
        return $output !== false ? $output : false;

    }

结果

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值