ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ //用文件来保存token public function settoken() { if ($this->exixts_token()) { if ($this->exprise_token()) { $token = $this->gettoken(); unlink('token.txt'); file_put_contents('token.txt', $token); } else { $token = file_get_contents('token.txt'); } } else { $token = $this->gettoken(); file_put_contents('token.txt', $token); } return $token; } ~~~ ~~~ //判断文件是否存在 public function exixts_token() { if (file_exists('token.txt')) { return true; } else { return false; } } ~~~ ~~~ //获取token.txt的创建时间并进行对比 public function exprise_token() { $ctime = filectime('token.txt'); if ((time() - $ctime) >= 7000) { return true; } else { return false; } } ~~~ ~~~ 获取token protected function settoken(){ $appid='wx9a88fc225a103797'; $secret='837756930f4f9a5ca768b82997df45f4'; $ch = curl_init(); $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $output = curl_exec($ch); if ($output === false) { echo curl_error($ch); } else { $obj = json_decode($output, JSON_FORCE_OBJECT); $token=$obj['access_token']; return $token; } curl_close($ch); } ~~~