action; $this->$action(); } /** * 初始化 * */ public function __construct(){ $this->objMemcached = new Memcached(); $this->thisTime = time(); $wechat_cfg = Config::getInstance()->get('wechat_cfg'); $this->appSecret = $wechat_cfg['appSecret']; $this->appId = $wechat_cfg['appId']; $this->jsApiTicketCacheName ='weixin_jsApiTicke'; $this->accessTokenCacheName = 'weixin_AccessToken'; } /** * 每小时执一次:php /www/index.php -cCmdLine_GetWeixinToken */ public function doDefault(){ # 统一执行 $accessToken = $this->getAccessToken(); $jsApiTicket = $this->getJsApiTicket($accessToken); print " 时间:".date("Y-m-d H:i:s")." 计算结果:\n accessToken: {$accessToken}\n jsApiTicket: {$jsApiTicket}\n "; } /** * 获取微信 AccessToken 存入 Memcached */ public function getAccessToken(){ $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)); $accessToken = $res->access_token; $this->objMemcached -> set($this->accessTokenCacheName, $accessToken); return $accessToken; } /** * 获取微信 JsApiTicket 存入 Memcached */ public function getJsApiTicket($accessToken){ $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode($this->httpGet($url)); $jsApiTicket = $res->ticket; $this->objMemcached -> set($this->jsApiTicketCacheName, $jsApiTicket); return $jsApiTicket; } /** * curl 请求 */ private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } }