微信公众号推送模板消息

1、获取access_token

  public function get_access_token(){
      //        http请求方式: GET
        $app_id = 'wx7969d821d8d6a12a';
        $secret = '868479e59cf464469f8dc9c7a1adae15';
        $curl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$app_id&secret=$secret";
        $access_token = file_get_contents($curl);
        // print_r($access_token);die();
      return $access_token = json_decode($access_token,true);
  }

 2、发送请求

    public function cs()
    {
        $access_token = $this->get_access_token();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token['access_token'];
        // print_r($url);die();
        $json_template = $this->cs_mode();
        // print_r($json_template);die();
        $res = $this->curl_post($url, urldecode($json_template));
      
//
    }

    public function cs_mode( $url = '')
    {
        //模板消息
        $template = [
            'touser' => '', //用户openid
            'template_id' => '',//模板id
            'miniprogram'=>[
                'appid'=>'',//跳转小程序appId
                 'usePath'=>false,//是否跳转小程序首页 false不跳转首页
             "pagepath"=> ""], //小程序页面路径不能带.html ,如果pagepath不行,可换成pagePath
            'topcolor' => "#7B68EE",//消息字体颜色
            'data' => [
                'first' => ['value' => urlencode('测试提醒')],
                'keyword1' => ['value' => urlencode('测试团队')], //keyword需要与配置的模板消息对应  //
                'keyword2' => ['value' => urlencode('8人')], //keyword需要与配置的模板消息对应  //
                'remark' => ['value' => urlencode('点击进入小程序')]
            ],
        ];
        return json_encode($template);
    }

    private function curl_post($url , $data=[]){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值