php微信開發 大神看看有沒有語法錯誤,能不能自動擷取到最新access_token

來源:互聯網
上載者:User
class Wechat_base_api{                private $appid='wx5d4b7d2e8bfd5793';        private $appsecret='4379ef7213c36eae5dc2ab91f9f6aced';        //建構函式        public function __construct($appid,$appsecret)        {            $this->appid = $appid;            $this->appsecret = $appsecret;        }            //擷取access_token(支援自動更新憑證)        public function get_access_token()        {            $this->last_time = 1408851194;            $access_token = "jIGpovtFGZDXCB_K2vqDPTA05zP7VWZaKIHXC_qZDqEiSGONWfCzQ45fI9aksl2L188yhtPpNB61iOBS4TTtgw";            if(time() > ($this->last_time + 7200))            {                //GET請求的地址                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}";                 $access_token_Arr =  $this->https_request($url);                 $this->last_time = time();                 return $access_token_Arr['access_token'];                        }            return $access_token;        }
//https請求(支援GET和POST)        protected function https_request($url,$data = null)        {            $ch = curl_init();            curl_setopt($ch,CURLOPT_URL,$url);            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);            if(!empty($data))            {                curl_setopt($ch,CURLOPT_POST,1);//類比POST                curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//POST內容            }            $outopt = curl_exec($ch);            curl_close($ch);            $outopt = json_decode($outopt,true);            return $outopt;        }}

回複內容:

class Wechat_base_api{                private $appid='wx5d4b7d2e8bfd5793';        private $appsecret='4379ef7213c36eae5dc2ab91f9f6aced';        //建構函式        public function __construct($appid,$appsecret)        {            $this->appid = $appid;            $this->appsecret = $appsecret;        }            //擷取access_token(支援自動更新憑證)        public function get_access_token()        {            $this->last_time = 1408851194;            $access_token = "jIGpovtFGZDXCB_K2vqDPTA05zP7VWZaKIHXC_qZDqEiSGONWfCzQ45fI9aksl2L188yhtPpNB61iOBS4TTtgw";            if(time() > ($this->last_time + 7200))            {                //GET請求的地址                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}";                 $access_token_Arr =  $this->https_request($url);                 $this->last_time = time();                 return $access_token_Arr['access_token'];                        }            return $access_token;        }
//https請求(支援GET和POST)        protected function https_request($url,$data = null)        {            $ch = curl_init();            curl_setopt($ch,CURLOPT_URL,$url);            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);            if(!empty($data))            {                curl_setopt($ch,CURLOPT_POST,1);//類比POST                curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//POST內容            }            $outopt = curl_exec($ch);            curl_close($ch);            $outopt = json_decode($outopt,true);            return $outopt;        }}

1.沒有語法錯誤
2.這是誰家的id和secret...

問題大的很

  • 到期時間寫死,會導致不停請求介面,可能會達到介面請求上限

  • access_token也是寫死,當達到介面請求上限後,返回都是到期錯誤的access_token

  • 說到底,access_token沒有緩衝起來。

下面這個是根據提供的範例程式碼更改改過的:

appId = $appId ? $appId : $this->appId;        $this->appSecret = $appSecret ? $appSecret : $this->appSecret;        $this->file_path = $file_path ? $file_path : $this->file_path;    }    /**     * 擷取AccessToken     * @return mixed     */    private function getAccessToken() {        $data = json_decode(file_get_contents($this->file_path));        if ($data->expire_time < time()) { //判斷本機存放區的access_token是否到期            // 如果是企業號用以下URL擷取access_token            // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";            // 其它類型公眾號使用這個URL擷取access_token            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";            //提供者,拉取access_token等資訊            $res = json_decode($this->httpGet($url));            //提取出access_token            $access_token = $res->access_token;            //判斷access_token是否存在,存在儲存到本地檔案中            if ($access_token) {                $data->expire_time = time() + 7190;                $data->access_token = $access_token;                $fp = fopen($this->file_path, "w");                fwrite($fp, json_encode($data));                fclose($fp);            }        } else { //本機存放區的access_token沒有到期            $access_token = $data->access_token;        }        //返回        return $access_token;    }    /**     * 拉取資料     * @param $url     * @return mixed|string     */    private function httpGet($url) {        //本地請求,則跳過請求        if('127.0.0.1' == $_SERVER['SERVER_ADDR']){            return '';        }        $curl = curl_init();        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);        curl_setopt($curl, CURLOPT_TIMEOUT, 500);        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;    }}

官方文檔中的 PHP 代碼,感覺坑比較多,自己一個個介面試過去太耗時了(別問我是怎麼知道的…)。

因為我們項目中用的是 CI 架構,在 GitHub 中這個項目:https://github.com/dodgepudding/wechat-php-sdk 的基礎上稍加改動,就完成了的整合。

看這份代碼比看官方文檔要快,我是這麼覺得的…

哥們 你們家的appid 跟appsecret 暴露了!!!
另外 access_token 是有次數限制的
記得緩衝下來,不然請求次數過多,會出錯的

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.