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...
問題大的很
下面這個是根據提供的範例程式碼更改改過的:
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 是有次數限制的
記得緩衝下來,不然請求次數過多,會出錯的