OAuth2\Service::callApiEndpoint PHP Method

callApiEndpoint() public method

call an api endpoint. automatically adds needed authorization headers with access token or parameters
public callApiEndpoint ( string $endpoint, string $method = 'GET', array $uriParameters = [], mixed $postBody = null, array $additionalHeaders = [] ) : string
$endpoint string
$method string default 'GET'
$uriParameters array optional
$postBody mixed optional, can be string or array
$additionalHeaders array
return string
    public function callApiEndpoint($endpoint, $method = 'GET', array $uriParameters = array(), $postBody = null, array $additionalHeaders = array())
    {
        $token = $this->_dataStore->retrieveAccessToken();
        //check if token is invalid
        if ($token->getLifeTime() && $token->getLifeTime() < time()) {
            $token = $this->refreshAccessToken($token);
        }
        $parameters = null;
        $authorizationMethod = $this->_configuration->getAuthorizationMethod();
        switch ($authorizationMethod) {
            case Service\Configuration::AUTHORIZATION_METHOD_HEADER:
                $additionalHeaders = array_merge(array('Authorization: OAuth ' . $token->getAccessToken()), $additionalHeaders);
                break;
            case Service\Configuration::AUTHORIZATION_METHOD_ALTERNATIVE:
                if ($method !== 'GET') {
                    if (is_array($postBody)) {
                        $postBody['oauth_token'] = $token->getAccessToken();
                    } else {
                        $postBody .= '&oauth_token=' . urlencode($token->getAccessToken());
                    }
                } else {
                    $uriParameters['oauth_token'] = $token->getAccessToken();
                }
                break;
            default:
                throw new Exception("Invalid authorization method specified");
                break;
        }
        if ($method !== 'GET') {
            if (is_array($postBody)) {
                $parameters = http_build_query($postBody);
            } else {
                $parameters = $postBody;
            }
        }
        if (!empty($uriParameters)) {
            $endpoint .= (strpos($endpoint, '?') !== false ? '&' : '?') . http_build_query($uriParameters);
        }
        $http = new HttpClient($endpoint, $method, $parameters, $additionalHeaders);
        $http->execute();
        return $http->getResponse();
    }