公眾號開發之發送模板資訊

  • 2019 年 12 月 17 日
  • 筆記

前陣子小程式項目中因為需要及時通知用戶,就打算對接小程式的模板通知.可是說是小程式在明年的一月份就不支援了.所以就放下了.

今天有需要在公眾號中發送模板消息.也是直接看了下文檔直接來對接起來.也是很順利.

首先,需要去公眾號後台申請模板.

在微信公眾平台-功能-模板消息裡面申請.選擇自己需要選擇的分類.保存下模板id.

這裡自己也是簡單封裝下.為以後需要作準備.這裡簡單分享下.

需要注意的是這裡的Accesstoken  你需要根據根據自己的情況來保存一下.因為這個東西每天只有2000次的獲取次數.保存到快取或者資料庫都可以.這裡做最簡單的分享

 /**       公眾號模板消息       * $openid   需要發送用戶的openid       * $name 用戶名字       * $mobile 聯繫方式       * $time 派單時間       * $content 維修內容       */      public function templateInfo($openid,$name,$mobile,$time,$content)      {          $ACCESS_TOKEN = $this->getAccessToken();//通過微信獲取access_token介面 獲取的token         // $openid = '';//用戶openid          $template_id = '';//配置的模板id          $url = '';//點擊模板消息跳轉的鏈接          $template = array(              'touser' => $openid,              'template_id' => $template_id,              'url' => $url,              'data' => array(                  'first' => array('value' => '您好,您有新的維修任務!', 'color' => "#173177"),                  'keyword1' => array('value' => $name, 'color' => '#173177'),                  'keyword2' => array('value' => $mobile, 'color' => '#173177'),                  'keyword3' => array('value' => $time, 'color' => '#173177'),                  'keyword4' => array('value' => $content, 'color' => '#173177'),),                  'remark' => array('value' => '請及時登錄公眾號接單確認!', 'color' => "#173177")          );            $send_template_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $ACCESS_TOKEN;            $ch = curl_init();          curl_setopt($ch, CURLOPT_URL, $send_template_url);          curl_setopt($ch, CURLOPT_HEADER, 0);          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);          curl_setopt($ch, CURLOPT_POST, 1);          curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($template));          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);          $res = curl_exec($ch);          curl_close($ch);          return $res;      }      /**       * 獲取access_token       */      public function getAccessToken()      {            $appId = db('config')->where(['name' => 'appid'])->value('value');          $appSecret = db('config')->where(['name' => 'appsearct'])->value('value');          $tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;          $getArr=array();          $tokenArr=json_decode($this->send_post($tokenUrl,$getArr,"GET"));          $access_token=$tokenArr->access_token;          return $access_token;      }        /**       * 請求方法       */      protected function send_post($url, $post_data,$method='POST')      {            $postdata = http_build_query($post_data);            $options = array(                'http' => array(                    'method' => $method, //or GET                    'header' => 'Content-type:application/x-www-form-urlencoded',                    'content' => $postdata,                    'timeout' => 15 * 60 // 超時時間(單位:s)                )            );          $context = stream_context_create($options);            $result = file_get_contents($url, false, $context);            return $result;      }

用的時候直接調用templateInfo 這個方法傳入對應的參數就行. ?

效果如圖下所示