protected function sendRequest($path, $method = 'GET', $data = array(), $useToken = true)
    {
        $url = $this->apiUrl . '/' . $path;
        $method = strtoupper($method);
        $curl = curl_init();

        if ($useToken && !empty($this->token)) {
            $headers = array('Authorization: Bearer ' . $this->token);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }

        switch ($method) {
            case 'POST':
                curl_setopt($curl, CURLOPT_POST, count($data));
                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
                break;
            case 'PUT':
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
                break;
            case 'DELETE':
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
                break;
            default:
                if (!empty($data)) {
                    $url .= '?' . http_build_query($data);
                }
        }

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15);
        curl_setopt($curl, CURLOPT_TIMEOUT, 15);

        $response = curl_exec($curl);
        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
        $headerCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $responseBody = substr($response, $header_size);

        curl_close($curl);

        if ($headerCode === 401 && $this->refreshToken === 0) {
            ++$this->refreshToken;
            $this->getToken();
            $retval = $this->sendRequest($path, $method, $data);
        } else {
            $retval = new stdClass();
            $retval->data = json_decode($responseBody);
            $retval->http_code = $headerCode;
        }

        return $retval;
    }
protected function sendRequest($path, $method = 'GET', $data = array(), $useToken = true)
    {
        $url = $this->apiUrl . '/' . $path;
        $method = strtoupper($method);
        $curl = curl_init();

        if ($useToken && !empty($this->token)) {
            $headers = array('Authorization: Bearer ' . $this->token);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }

        switch ($method) {
            case 'POST':
                curl_setopt($curl, CURLOPT_POST, count($data));
                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
                break;
            case 'PUT':
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
                break;
            case 'DELETE':
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
                break;
            default:
                if (!empty($data)) {
                    $url .= '?' . http_build_query($data);
                }
        }

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15);
        curl_setopt($curl, CURLOPT_TIMEOUT, 15);

        $response = curl_exec($curl);
        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
        $headerCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $responseBody = substr($response, $header_size);

        curl_close($curl);

        if ($headerCode === 401 && $this->refreshToken === 0) {
            ++$this->refreshToken;
            $this->getToken();
            $retval = $this->sendRequest($path, $method, $data);
        } else {
            $retval = new stdClass();
            $retval->data = json_decode($responseBody);
            $retval->http_code = $headerCode;
        }

        return $retval;
    }
protected function sendRequest ( $ path , $ method = 'GET' , $ data = array ( ) , $ useToken = true ) { $ url = $ this -> apiUrl . '/' . $ path ; $ method = strtoupper ( $ method ) ; $ curl = curl_init ( ) ; if ( $ useToken && ! empty ( $ this -> token ) ) { $ headers = array ( 'Authorization: Bearer ' . $ this -> token ) ; curl_setopt ( $ curl , CURLOPT_HTTPHEADER , $ headers ) ; } switch ( $ method ) { case 'POST' : curl_setopt ( $ curl , CURLOPT_POST , count ( $ data ) ) ; curl_setopt ( $ curl , CURLOPT_POSTFIELDS , http_build_query ( $ data ) ) ; break ; case 'PUT' : curl_setopt ( $ curl , CURLOPT_CUSTOMREQUEST , 'PUT' ) ; curl_setopt ( $ curl , CURLOPT_POSTFIELDS , http_build_query ( $ data ) ) ; break ; case 'DELETE' : curl_setopt ( $ curl , CURLOPT_CUSTOMREQUEST , 'DELETE' ) ; curl_setopt ( $ curl , CURLOPT_POSTFIELDS , http_build_query ( $ data ) ) ; break ; default : if ( ! empty ( $ data ) ) { $ url .= '?' . http_build_query ( $ data ) ; } } curl_setopt ( $ curl , CURLOPT_URL , $ url ) ; curl_setopt ( $ curl , CURLOPT_SSL_VERIFYPEER , false ) ; curl_setopt ( $ curl , CURLOPT_SSL_VERIFYHOST , false ) ; curl_setopt ( $ curl , CURLOPT_RETURNTRANSFER , true ) ; curl_setopt ( $ curl , CURLOPT_HEADER , true ) ; curl_setopt ( $ curl , CURLOPT_CONNECTTIMEOUT , 15 ) ; curl_setopt ( $ curl , CURLOPT_TIMEOUT , 15 ) ; $ response = curl_exec ( $ curl ) ; $ header_size = curl_getinfo ( $ curl , CURLINFO_HEADER_SIZE ) ; $ headerCode = curl_getinfo ( $ curl , CURLINFO_HTTP_CODE ) ; $ responseBody = substr ( $ response , $ header_size ) ; curl_close ( $ curl ) ; if ( $ headerCode === 401 && $ this -> refreshToken === 0 ) { ++ $ this -> refreshToken ; $ this -> getToken ( ) ; $ retval = $ this -> sendRequest ( $ path , $ method , $ data ) ; } else { $ retval = new stdClass ( ) ; $ retval -> data = json_decode ( $ responseBody ) ; $ retval -> http_code = $ headerCode ; } return $ retval ; }
Form and send request to API service

@param        $path
@param string $method
@param array $data
@param bool $useToken

@return stdClass
Form and send request to API service