env = 'd'; } elseif (false !== strpos($_SERVER['HTTP_HOST'], 'api.lc.haodian.cn')) {//test 测试 $this->env = 't'; } else { // 正式 $this->env = 'p'; } if ('p' != $this->env) { $this->url_wechat = "https://api.liche.cn/weixin"; } $this->redis = &load_cache('redis'); $param && $this->init($param); } /** * 初始化 * @param $param */ function init($param) { $this->appid = $param['appid']; $this->secret = $param['secret']; $this->log_dir = "libraries_wechat_{$this->appid}"; } /** * 获取或者重置access_token * @param $reset (是否重置) * @return mixed */ function access_token($reset = false) { $redis = $this->redis; $key = "wechat_access_token_{$this->appid}"; debug_log("[info]: appid={$this->appid}", __FUNCTION__, $this->log_dir); $func = __FUNCTION__; if (!$reset) {//不重置 if ($this->access_token) { $access_token = $this->access_token; } elseif ($this->url_wechat) {//从线上获取access_token $url = $this->url_wechat . "/{$func}?appid={$this->appid}&secret={$this->secret}"; $res = $this->http_get($url); $ret = json_decode($res, true); $access_token = $ret['access_token']; if (!$access_token) { debug_log("[warning]# res={$res}; url={$url}", __FUNCTION__, $this->log_dir); } } else {//缓存中获取 $access_token = $redis->get($key); } if ($access_token) { return $access_token; } } //重置access_token,第三方的不处理 if ($this->url_wechat) { $url = $this->url_wechat . "/{$func}?appid={$this->appid}&secret={$this->secret}&reset=1"; } else { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->secret}"; } $res = $this->http_get($url); $ret = json_decode($res, true); $access_token = $ret['access_token']; if ($access_token) { //线上环境才缓存 'p' == $this->env && $redis->save($key, $access_token, 7000); } else { debug_log("[warning]# res={$res}; url={$url}", __FUNCTION__, $this->log_dir); } $this->access_token = $access_token; return $access_token; } /** * 分享票据 * @param bool $reset "是否重置" * @return mixed */ function api_ticket($reset = false) { $redis = $this->redis; $key = 'wechat_jsApiTicket_' . $this->appid; $func = __FUNCTION__; if (!$reset) {//不重置 if ($this->api_ticket) { $api_ticket = $this->api_ticket; } elseif ($this->url_wechat) {//从线上获取ticket $url = $this->url_wechat . "/{$func}?appid={$this->appid}&secret={$this->secret}"; $res = $this->http_get($url); $ret = json_decode($res, true); $api_ticket = $ret['api_ticket']; if (!$api_ticket) { debug_log("[warning]# res={$res}; url={$url}", __FUNCTION__, $this->log_dir); } } else {//缓存中获取 $api_ticket = $redis->get($key); } if ($api_ticket) { return $api_ticket; } } //重置 if ($this->url_wechat) {//从线上重置获取ticket $url = $this->url_wechat . "/{$func}?appid={$this->appid}&secret={$this->secret}&reset=1"; $res = $this->http_get($url); $ret = json_decode($res, true); } else { $access_token = $this->access_token(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token={$access_token}"; $res = $this->http_get($url); $ret = json_decode($res, true); if (!$ret['ticket']) { debug_log("[warning]# res={$res}; url={$url}", __FUNCTION__, $this->log_dir); //可能是access_token过期,重置获取一下 $access_token = $this->access_token(true); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token={$access_token}"; $res = $this->http_get($url); $ret = json_decode($res, true); } } $api_ticket = $ret['ticket']; if ($api_ticket) { //线上环境才缓存 'p' == $this->env && $redis->save($key, $api_ticket, 7000); } else { debug_log("[warning]# res={$res}; url={$url}", __FUNCTION__, $this->log_dir); } $this->api_ticket = $api_ticket; return $api_ticket; } /** * @param $url * @return mixed */ private function http_get($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if (200 != $code) { $size = strlen($res); if ($size > 5000) { debug_log("[info]# httpcode={$code}, response is big only show size={$size}", __FUNCTION__, $this->log_dir); } else { debug_log("[info]# httpcode={$code}, response={$res}", __FUNCTION__, $this->log_dir); } return ""; } return $res; } }