_HttpPostParseToJson($url, $args); $this->_CheckErrCode(); } else if ('GET' == $method) { if (count($args) > 0) { foreach ($args as $key => $value) { if ($value == null) continue; if (strpos($url, '?')) { $url .= ('&'.$key.'='.$value); } else { $url .= ('?'.$key.'='.$value); } } } $url = HttpUtils::MakeUrl($url); $this->_HttpGetParseToJson($url); $this->_CheckErrCode(); } else { throw new QyApiError('wrong method'); } } protected function _HttpGetParseToJson($url, $refreshTokenWhenExpired=true) { $retryCnt = 0; $this->rspJson = null; $this->rspRawStr = null; while ($retryCnt < 2) { $tokenType = null; $realUrl = $url; if (strpos($url, "SUITE_ACCESS_TOKEN")) { $token = $this->GetSuiteAccessToken(); $realUrl = str_replace("SUITE_ACCESS_TOKEN", $token, $url); $tokenType = "SUITE_ACCESS_TOKEN"; } else if (strpos($url, "PROVIDER_ACCESS_TOKEN")) { $token = $this->GetProviderAccessToken(); $realUrl = str_replace("PROVIDER_ACCESS_TOKEN", $token, $url); $tokenType = "PROVIDER_ACCESS_TOKEN"; } else if (strpos($url, "ACCESS_TOKEN")) { $token = $this->GetAccessToken(); $realUrl = str_replace("ACCESS_TOKEN", $token, $url); $tokenType = "ACCESS_TOKEN"; } else { $tokenType = "NO_TOKEN"; } $this->rspRawStr = HttpUtils::httpGet($realUrl); if ( ! Utils::notEmptyStr($this->rspRawStr)) throw new QyApiError("empty response"); // $this->rspJson = json_decode($this->rspRawStr, true/*to array*/); if (strpos($this->rspRawStr, "errcode") !== false) { $errCode = Utils::arrayGet($this->rspJson, "errcode"); if ($errCode == 40014 || $errCode == 42001 || $errCode == 42007 || $errCode == 42009) { // token expired if ("NO_TOKEN" != $tokenType && true == $refreshTokenWhenExpired) { if ("ACCESS_TOKEN" == $tokenType) { $this->RefreshAccessToken(); } else if ("SUITE_ACCESS_TOKEN" == $tokenType) { $this->RefreshSuiteAccessToken(); } else if ("PROVIDER_ACCESS_TOKEN" == $tokenType) { $this->RefreshProviderAccessToken(); } $retryCnt += 1; continue; } } } return $this->rspRawStr; } } protected function _HttpPostParseToJson($url, $args, $refreshTokenWhenExpired=true, $isPostFile=false) { $postData = $args; if (!$isPostFile) { if (!is_string($args)) { $postData = HttpUtils::Array2Json($args); } } $this->rspJson = null; $this->rspRawStr = null; $retryCnt = 0; while ($retryCnt < 2) { $tokenType = null; $realUrl = $url; if (strpos($url, "SUITE_ACCESS_TOKEN")) { $token = $this->GetSuiteAccessToken(); $realUrl = str_replace("SUITE_ACCESS_TOKEN", $token, $url); $tokenType = "SUITE_ACCESS_TOKEN"; } else if (strpos($url, "PROVIDER_ACCESS_TOKEN")) { $token = $this->GetProviderAccessToken(); $realUrl = str_replace("PROVIDER_ACCESS_TOKEN", $token, $url); $tokenType = "PROVIDER_ACCESS_TOKEN"; } else if (strpos($url, "ACCESS_TOKEN")) { $token = $this->GetAccessToken(); $realUrl = str_replace("ACCESS_TOKEN", $token, $url); $tokenType = "ACCESS_TOKEN"; } else { $tokenType = "NO_TOKEN"; } $this->rspRawStr = HttpUtils::httpPost($realUrl, $postData); if ( ! Utils::notEmptyStr($this->rspRawStr)) throw new QyApiError("empty response"); $json = json_decode($this->rspRawStr, true/*to array*/); $this->rspJson = $json; $errCode = Utils::arrayGet($this->rspJson, "errcode"); if ($errCode == 40014 || $errCode == 42001 || $errCode == 42007 || $errCode == 42009) { // token expired if ("NO_TOKEN" != $tokenType && true == $refreshTokenWhenExpired) { if ("ACCESS_TOKEN" == $tokenType) { $this->RefreshAccessToken(); } else if ("SUITE_ACCESS_TOKEN" == $tokenType) { $this->RefreshSuiteAccessToken(); } else if ("PROVIDER_ACCESS_TOKEN" == $tokenType) { $this->RefreshProviderAccessToken(); } $retryCnt += 1; continue; } } return $json; } } protected function _CheckErrCode() { $rsp = $this->rspJson; $raw = $this->rspRawStr; if (is_null($rsp)) return; if (!is_array($rsp)) throw new ParameterError("invalid type " . gettype($rsp)); if (!array_key_exists("errcode", $rsp)) { return; } $errCode = $rsp["errcode"]; if (!is_int($errCode)) throw new QyApiError( "invalid errcode type " . gettype($errCode) . ":" . $raw); if ($errCode != 0) throw new QyApiError("response error:" . $raw); } }