/** * 检验授权凭证(access_token)是否有效 * * @param string $access_token * @param string $openid * @return mixed */ public function checkAccessToken($access_token, $openid) { $url = 'https://api.weixin.qq.com/sns/auth?access_token=' . $access_token . '&openid=' . $openid; return spWxHttpUtil::http_get($url); }
/** * JS SDK ticket * * @param string $request_url * @param string $type jsapi, wx_card * @return bool|mixed|string * @throws \spWxException */ public function getJSSDKTicket($request_url = '', $type = 'jsapi') { $token = $this->getAccessToken(); $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=' . $type . '&access_token=' . $token; $cache_key = 'wx:ticket:' . $type . ':' . md5($url); $jsapi_ticket = spWxCache::get($cache_key); $jsapi_ticket = json_decode($jsapi_ticket, true); if (!$jsapi_ticket) { $jsapi_ticket = spWxHttpUtil::http_get($url); spWxLogger::LogHttpGet($url, $jsapi_ticket); if (isset($jsapi_ticket['errcode']) && $jsapi_ticket['errcode'] == '40001') { $this->delAccessToken(); return $this->getJSSDKTicket($request_url); } if (!isset($jsapi_ticket['ticket']) || !isset($jsapi_ticket['expires_in'])) { throw new spWxException('服务器故障,请稍后重试', 9210201); } spWxCache::set($cache_key, json_encode($jsapi_ticket), $jsapi_ticket['expires_in'] - 300); } $noncestr = md5(uniqid('sskaje', true)); $jsapi_ticket['noncestr'] = $noncestr; $jsapi_ticket['timestamp'] = time(); $signstr = 'jsapi_ticket=' . $jsapi_ticket['ticket'] . '&noncestr=' . $jsapi_ticket['noncestr'] . '×tamp=' . $jsapi_ticket['timestamp']; if (empty($request_url)) { $signstr .= '&url=' . 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } else { $signstr .= '&url=' . $request_url; } $jsapi_ticket['signature'] = sha1($signstr); $jsapi_ticket['signstr'] = $signstr; $jsapi_ticket['appid'] = $this->app_id; return $jsapi_ticket; }