天天看點

php之微信公衆号發送模闆消息參觀模仿

上篇文章中鞋到了公衆号發送末班消息,他是最後調用兩個方法,本文章簡化一下

将下面的php方法放到一個可以引入的公共類中即可

構模組化闆消息:

我把需要用到的模闆消息 都寫成一個個方法,放在公共類檔案中了,可以參考一下:

//發送模闆消息給使用者
    public function sendanswertpltouser($openid, $template_id, $url, $first, $keynote1, $keynote2, $keynote3, $remark)
    {
        $data        = '
           {
                     "touser":"' . $openid . '",
                     "template_id":"' . $template_id . '",
                     "url":"' . $url . '",
                     "miniprogram":{
                       "appid":"",
                       "pagepath":""
                     },
                     "data":{
                             "first": {
                                 "value":"' . $first . '",
                                 "color":"#bc723c"
                             },
                             "keyword1":{
                                 "value":"' . $keynote1 . '"

                             },
                             "keyword2": {
                                 "value":"' . $keynote2 . '"

                             },
                             "keyword3": {
                                 "value":"' . $keynote3 . '"

                             },
                             "remark":{
                                 "value":"' . $remark . '",
                                 "color":"#173177"
                             }
                     }
                 }
          ';
        $accessToken = $this->getAccessToken();
        $url         = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;
        $result      = $this->https_request($url, $data);
        return json_decode($result, true);
    }      

這隻是我寫的其中一個下單成功的模闆消息,放在common檔案中了,這樣基本上就完成了:

<?php

class JSSDK
{
    private $appId;
    private $appSecret;

    public function __construct($appId, $appSecret)
    {
        $this->appId     = $appId;
        $this->appSecret = $appSecret;
    }

    public function getSignPackage()
    {
        $jsapiTicket = $this->getJsApiTicket();

        // 注意 URL 一定要動态擷取,不能 hardcode.
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url      = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

        $timestamp = time();
        $nonceStr  = $this->createNonceStr();

        // 這裡參數的順序要按照 key 值 ASCII 碼升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

        $signature = sha1($string);

        $signPackage = array(
            "appId" => $this->appId,
            "nonceStr" => $nonceStr,
            "timestamp" => $timestamp,
            "url" => $url,
            "signature" => $signature,
            "rawString" => $string
        );
        return $signPackage;
    }

    private function createNonceStr($length = 16)
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str   = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    private function getJsApiTicket()
    {
        // jsapi_ticket 應該全局存儲與更新,以下代碼以寫入到檔案中做示例
        $data = json_decode($this->get_php_file("jsapi_ticket.php"));
        if ($data->expire_time < time()) {
            $accessToken = $this->getAccessToken();
            // 如果是企業号用以下 URL 擷取 ticket
            // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
            $url    = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
            $res    = json_decode($this->httpGet($url));
            $ticket = $res->ticket;
            if ($ticket) {
                $data->expire_time  = time() + 7000;
                $data->jsapi_ticket = $ticket;
                $this->set_php_file("jsapi_ticket.php", json_encode($data));
            }
        } else {
            $ticket = $data->jsapi_ticket;
        }

        return $ticket;
    }

    public function getAccessToken()
    {
        // access_token 應該全局存儲與更新,以下代碼以寫入到檔案中做示例
        $data = json_decode($this->get_php_file("access_token.php"));

        if ($data->expire_time < time()) {

            // 如果是企業号用以下URL擷取access_token
            // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
            $res = json_decode($this->httpGet($url));

            $access_token = $res->access_token;
            if ($access_token) {

                $data->expire_time  = time() + 7000;
                $data->access_token = $access_token;
                $this->set_php_file("access_token.php", json_encode($data));
            }
        } else {
            $access_token = $data->access_token;
        }

        return $access_token;
    }

//擷取使用者基本資訊
    public function get_user_info($openid)
    {
        $accessToken = $this->getAccessToken();
        $url         = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $accessToken . "&openid=" . $openid . "&lang=zh_CN";
        $res         = $this->https_request($url);
        return json_decode($res, true);
    }

    //發送模闆消息給使用者--提現申請
    public function sendtixianshengqingtpltouser($openid, $template_id, $url, $first, $keynote1, $keynote2, $keynote3, $keynote4, $remark)
    {
        $data  = '
 {
            "touser":"' . $openid . '",
           "template_id":"' . $template_id . '",
           "url":"' . $url . '",
           "miniprogram":{
             "appid":"",
             "pagepath":""
           },
           "data":{
                   "first": {
                       "value":"' . $first . '",
                       "color":"#bc723c"
                   },
                   "keyword1":{
                       "value":"' . $keynote1 . '"

                   },
                   "keyword2": {
                       "value":"' . $keynote2 . '"

                   },
                   "keyword3": {
                       "value":"' . $keynote3 . '"

                   },
                     "keyword4": {
                       "value":"' . $keynote4 . '"

                   },
                   "remark":{
                       "value":"' . $remark . '",
                       "color":"#173177"
                   }
           }
       }
';
        $accessToken = $this->getAccessToken();
        $url         = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;
        $result      = $this->https_request($url, $data);
        return json_decode($result, true);
    }

    //發送模闆消息給使用者--提現稽核失敗
    public function sendtixianjieguotpltouser($openid, $template_id, $url, $first, $keynote1, $keynote2, $keynote3, $keynote4, $keynote5, $remark)
    {
        $data        = '
 {
            "touser":"' . $openid . '",
           "template_id":"' . $template_id . '",
           "url":"' . $url . '",
           "miniprogram":{
             "appid":"",
             "pagepath":""
           },
           "data":{
                   "first": {
                       "value":"' . $first . '",
                       "color":"#bc723c"
                   },
                   "keyword1":{
                       "value":"' . $keynote1 . '"

                   },
                   "keyword2": {
                       "value":"' . $keynote2 . '"

                   },
                   "keyword3": {
                       "value":"' . $keynote3 . '"

                   },
                     "keyword4": {
                       "value":"' . $keynote4 . '"

                   },

                     "keyword5": {
                       "value":"' . $keynote5 . '"

                   },
                   "remark":{
                       "value":"' . $remark . '",
                       "color":"#173177"
                   }
           }
       }
';
        $accessToken = $this->getAccessToken();
        $url         = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;
        $result      = $this->https_request($url, $data);
        return json_decode($result, true);
    }

    //發送模闆消息給使用者
    public function sendanswertpltouser($openid, $template_id, $url, $first, $keynote1, $keynote2, $keynote3, $remark)
    {
        $data        = '
           {
                     "touser":"' . $openid . '",
                     "template_id":"' . $template_id . '",
                     "url":"' . $url . '",
                     "miniprogram":{
                       "appid":"",
                       "pagepath":""
                     },
                     "data":{
                             "first": {
                                 "value":"' . $first . '",
                                 "color":"#bc723c"
                             },
                             "keyword1":{
                                 "value":"' . $keynote1 . '"

                             },
                             "keyword2": {
                                 "value":"' . $keynote2 . '"

                             },
                             "keyword3": {
                                 "value":"' . $keynote3 . '"

                             },
                             "remark":{
                                 "value":"' . $remark . '",
                                 "color":"#173177"
                             }
                     }
                 }
          ';
        $accessToken = $this->getAccessToken();
        $url         = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;
        $result      = $this->https_request($url, $data);
        return json_decode($result, true);
    }

    //發送消息給使用者
    public function sendtexttouser($openid, $msg)
    {
        $data        = '{
         "touser":"' . $openid . '",
         "msgtype":"text",
         "text":
         {
           "content":"' . $msg . '"
         }
        }';
        $accessToken = $this->getAccessToken();
        $url         = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $accessToken;
        $result      = $this->https_request($url, $data);
        return json_decode($result, true);
    }

    //https請求
    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;
    }

    //傳回使用者清單資訊
    public function getuserlist($next_openid = "")
    {

        $accessToken = $this->getAccessToken();

        $url = $next_openid == "" ? "https://api.weixin.qq.com/cgi-bin/user/get?access_token=$accessToken" : "https://api.weixin.qq.com/cgi-bin/user/get?access_token=$accessToken&next_openid=$next_openid";
        $res = $this->httpGet($url);
        return json_decode($res, true);
    }

    //傳回網頁授權url
    private function getoauthurl($REDIRECT_URI, $SCOPE)
    {
        $appId = $this->appId;


        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appId&redirect_uri=$REDIRECT_URI&response_type=code&scope=$SCOPE&state=STATE#wechat_redirect";
    }


    private function httpGet($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        // 為保證第三方伺服器與微信伺服器之間資料傳輸的安全性,所有微信接口采用https方式調用,必須使用下面2行代碼打開ssl安全校驗。
        // 如果在部署過程中代碼在此處驗證失敗,請到 http://curl.haxx.se/ca/cacert.pem 下載下傳新的證書判别檔案。
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);

        $res = curl_exec($curl);

        curl_close($curl);

        return $res;
    }

    private function get_php_file($filename)
    {
        return trim(substr(file_get_contents(FCPATH . 'lib/php/' . $filename), 15));
    }

    private function set_php_file($filename, $content)
    {
        $fp = fopen(FCPATH . 'lib/php/' . $filename, "w");
        fwrite($fp, "<?php exit();?>" . $content);
        fclose($fp);
    }
}