微信公众号推送模板消息

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

猜你喜欢

转载自blog.csdn.net/weixin_45604963/article/details/127771412