/** * Executes multi curl requests and return response. Response is returned in an array and * result array contains responses in same order in what requests were added. Response array format is * array(array(self::SRV_CODE => 'http_response_code', self::SRV_RESPONSE => 'response', * self::SRV_ERROR_CODE => 'any error code', self::SRV_ERROR => 'any error string'), ...) * * @param int $_maxConcurrent Executes this numbers of requests in parallel * @author Atul Atri * @return array e.g array() */ public function ExecuteMultiCurl($_maxConcurrent = 10, $_isRetry = false) { $_multiCurlResponseArr = parent::ExecuteMultiCurl($_maxConcurrent); $_isAll401 = true; foreach ($_multiCurlResponseArr as $_nextResponse) { if ($_nextResponse[self::SRV_CODE] != 401) { $_isAll401 = false; break; } } if (!$_isRetry && $_isAll401) { $this->_reqRetryCount = 0; } if ($_isAll401 == 401 && $this->_reqRetryCount < $this->_maxSendReqRetries) { $this->_CurlCurrentIndex = 0; //most probably token expired...ask oauth lib to regenerate token $_refreshed = SWIFT_APIOauth2::RefreshToken(); if ($_refreshed) { $this->_reqRetryCount++; $this->ModifyAuthHeader(); return $this->ExecuteMultiCurl($_maxConcurrent, true); } } return $_multiCurlResponseArr; }
public static function GetInstance() { if (!isset(self::$_Instance) || get_class(self::$_Instance) != get_called_class()) { $_class = get_called_class(); self::$_Instance = new $_class(); } return self::$_Instance; }
/** * Execute access token request and return response retuned by server * * @param string $_tokenEndPoint token end point url * @param array $_params list of parameters required to execute request * @param int $_clientAuthType client authentication type * @param array any additional header that needs to be sent e.g. array('Conent-Type' =>'application/json'); * @param string method method type e.g. 'GET', 'POST', 'DELETE', 'PUT' * * @return mixed response retuned by server * @throws SWIFT_OAuth2Exception if failed to execute request */ public function AccessTokenRequest($_tokenEndPoint, array $_params, $_clientAuthType = self::AUTH_TYPE_AUTHORIZATION_FORM, array $_headers = array(), $_method = 'POST') { $_params = $this->CheckTokenRequestParams($_params); if (($_clientAuthType == self::AUTH_TYPE_AUTHORIZATION_FORM || $_clientAuthType == self::AUTH_TYPE_AUTHORIZATION_BASIC) && (!isset($_params['client_id']) || !isset($_params['client_secret']))) { throw new SWIFT_OAuth2Exception('AUTH_TYPE_AUTHORIZATION_FORM requires client_id and client_secret to be given in params'); } if ($_clientAuthType == self::AUTH_TYPE_AUTHORIZATION_BASIC && !isset($_headers['Authorization'])) { $_headers['Authorization'] = 'Basic ' . base64_encode($_params['client_id'] . ':' . $_params['client_secret']); } $_params = $this->ChangeParamNames($_params); //execute request $_CurlInstance = SWIFT_HttpCurl::GetInstance(); $_paramsStr = http_build_query($_params); $_defaultHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; $_headers = $_headers + $_defaultHeaders; return $_CurlInstance->SendRequest($_tokenEndPoint, $_paramsStr, $_method, $_headers); }