OAuth2Client::api PHP Method

api() public method

Format and sign an oauth for provider api
public api ( $url, $method = "GET", $parameters = [], $decode_json = true )
    public function api($url, $method = "GET", $parameters = array(), $decode_json = true)
    {
        if (strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0) {
            $url = $this->api_base_url . $url;
        }
        $parameters[$this->sign_token_name] = $this->access_token;
        $response = null;
        switch ($method) {
            case 'GET':
                $response = $this->request($url, $parameters, "GET");
                break;
            case 'POST':
                $response = $this->request($url, $parameters, "POST");
                break;
            case 'DELETE':
                $response = $this->request($url, $parameters, "DELETE");
                break;
            case 'PATCH':
                $response = $this->request($url, $parameters, "PATCH");
                break;
        }
        if ($response && $decode_json) {
            return $this->response = json_decode($response);
        }
        return $this->response = $response;
    }

Usage Example

 public function api($path, $method = 'GET', $params = array())
 {
     try {
         return parent::api($path, $method, $params);
     } catch (OAuth2Exception $e) {
         //once and only once, try to get use the refresh token to get a fresh token
         if ($e->getMessage() == 'INVALID_SESSION_ID') {
             $this->refreshToken();
             return parent::api($path, $method, $params);
         } else {
             throw $e;
         }
     }
 }