public function dispatch($ignoreErrors = true, $timeout = null) {

        if (!isset($this->headers)) {
            $this->headers = array();
        }

        if (!isset($this->headers["Content-Type"]))
            $this->headers["Content-Type"] = "application/json";


        // If we have an auth username and password, use it.
        if ($this->authUsername && $this->authPassword) {
            $this->headers["Authorization"] = "Basic " . base64_encode($this->authUsername . ":" . $this->authPassword);
        }

        $headers = array();
        foreach ($this->headers as $key => $header) {
            $headers[] = $key . ": " . $header;
        }

        if (is_array($this->parameters))
            $queryParams = http_build_query($this->parameters);

        if ($this->payload) {
            $payload = $this->payload;
        } else {
            $payload = null;
        }

        $paramsAsGet = $payload || $this->method == "GET";
        $contentData = $payload ? $payload : ($paramsAsGet ? array() : $queryParams);

        $options = array('http' => array('header' => $headers, 'method' => $this->method,
            'content' => $contentData, 'ignore_errors' => $ignoreErrors));

        $url = $this->url;
        if ($paramsAsGet && sizeof($this->parameters) > 0) {
            $url .= "?" . $queryParams;
        }

        if ($timeout) {
            $options["timeout"] = $timeout;
        }

        $context = stream_context_create($options);

        $results = file_get_contents($url, false, $context);

        $responseCode = explode(" ", $http_response_header[0])[1];

        if ($responseCode >= 400) {
            throw new HttpRequestErrorException($url, $responseCode, $results);
        }


        return $results;
    }