/** * 获取微信ticket * * @param string $appId * @param string $appSercet * @param bool $refresh 如果cache中不存在是否刷新cache * @return string */ function getJsApiTicket($appId, $appSecret, $type = 'jsapi', $refresh = true) { $type = in_array($type, array('jsapi', 'wx_card')) ? $type : 'jsapi'; if ('jsapi' == $type) { $cacherId = SuiShiPHPConfig::get('WX_JS_API_TICKET') . $appId; } else { $cacherId = SuiShiPHPConfig::get('WX_CARD_API_TICKET') . $appId; } $cacher = new RedisCache(SuiShiPHPConfig::get('REDIS_HOST_TOKEN'), SuiShiPHPConfig::get('REDIS_PORT_TOKEN')); $ticket = $cacher->get ( $cacherId ); //TODO test //$token = '2FsxZXKoX6NAS9eV28UIZQz3YwoPXvBf2Gjr1O8bNl9nzKBpZub7_1zZ4gsWC1_LdzcwAJ7lW9oWLDghMWXvAn3w3Gcj63pX7ljpHprqCUE'; if (true !== $refresh) { return $ticket; } if (! $ticket) { // 引入微信api if (! class_exists ( "WeiXinClient" )) { include_once dirname ( __FILE__ ) . "/../API/WeiXinApiCore.class.php"; } $token = getToken($appId, $appSecret); $weixnApi = WeiXinApiCore::getClient ( $appId, $appSecret, $token); $ticket = $weixnApi->getJsApiTicket ($type); if ($ticket) { $cacher->set ( $cacherId, $ticket, 6600/*一小时50分钟*/); } } return $ticket; }
/** * 获取微信token * * @param string $appId * @param string $appSercet * @param bool $refresh 如果cache中不存在是否刷新cache * @return string */ function getToken($appId, $appSecret, $refresh = true) { $cacherId = GlobalCatchId::WX_API_TOKEN . $appId; if ((defined ( "IS_LOCAL_MODULE" ) && IS_LOCAL_MODULE === true) || strtoupper ( substr ( PHP_OS, 0, 3 ) ) === 'WIN') { if ($appId == 'wx1f82add86fc7b5a5') { $data = @file_get_contents ( "http://call.socialjia.com/t.php?id=" . $cacherId ); $data = trim ( $data ); if (empty ( $data ) || ! $data = @unserialize ( $data )) { return null; } return @$data ['data']; } $cacher_host = ConfigBase::LOCAL_REMOTE_CACHER_HOST; $cacher_port = ConfigBase::LOCAL_REMOTE_CACHER_PORT; } else { $cacher_host = ConfigBase::REMOTE_CACHER_HOST; $cacher_port = ConfigBase::REMOTE_CACHER_PORT; } // 引入cacher if (false != ConfigBase::PUBLIC_SERVICE && class_exists('Redis')) { $cacher = Factory::getGlobalCacher(); } else { if (! class_exists ( "RemoteCacher" )) { include_once dirname ( __FILE__ ) . "/../Cache/RemoteCacher.class.php"; } $cacher = new RemoteCacher ( $cacher_host, $cacher_port, 'weixin' ); } $token = $cacher->get ( $cacherId ); if (true !== $refresh) { return $token; } if (! $token) { // 引入微信api if (! class_exists ( "WeiXinClient" )) { include_once dirname ( __FILE__ ) . "/../Api/WeiXinApiCore.class.php"; } $weixnApi = WeiXinApiCore::getClient ( $appId, $appSecret ); $token = $weixnApi->getToken (); if ($token) { $token = $token->token; $cacher->set ( $cacherId, $token, GlobalCatchExpired::WX_API_TOKEN ); } else { if (class_exists("Logger")) { Logger::error("fun::getToken error: ".$weixnApi->getErrorMessage()."; code:".$weixnApi->getErrorCode()); } } } return $token; }
public function getJsApiTicket($type = 'jsapi') { $type = in_array($type, array('jsapi', 'wx_card')) ? $type : 'jsapi'; if ('jsapi' == $type) { $cacherId = SuiShiPHPConfig::get('WX_JS_API_TICKET') . $this->appId; } else { $cacherId = SuiShiPHPConfig::get('WX_CARD_API_TICKET') . $this->appId; } $cacher = new RedisCache(SuiShiPHPConfig::get('REDIS_HOST_TOKEN'), SuiShiPHPConfig::get('REDIS_PORT_TOKEN')); $ticket = $cacher->get ( $cacherId ); if (! $ticket) { $token = $this->_getAccessToken(); if (! $token) { return false; } // 引入微信api if (! class_exists ( "WeiXinClient" )) { include_once dirname ( __FILE__ ) . "/../API/WeiXinApiCore.class.php"; } $weixinApi = WeiXinApiCore::getClient ($this->appId, $this->appSecret, $token); $ticket = $weixinApi->getJsApiTicket($type); if ($ticket) { $cacher->set ($cacherId, $ticket, 7000/*一小时56分钟*/); } } return $ticket; }