Single HTTP Request

@param string $url The URL to request
@param array $options
@return bool
437434
protected function request($url, $options = array()) {
        // Reset here so that the data is valid when result returned from cache, or if we return due to a blacklist hit.
        $this->reset_request_state_vars();

        if ((defined('PHPUNIT_TEST') && PHPUNIT_TEST)) {
            if ($mockresponse = array_pop(self::$mockresponses)) {
                $this->info = [ 'http_code' => 200 ];
                return $mockresponse;
            }
        }

        // If curl security is enabled, check the URL against the blacklist before calling curl_exec.
        // Note: This will only check the base url. In the case of redirects, the blacklist is also after the curl_exec.
        if (!$this->ignoresecurity && $this->securityhelper->url_is_blocked($url)) {
            $this->error = $this->securityhelper->get_blocked_url_string();
            return $this->error;
        }

        // Set the URL as a curl option.
        $this->setopt(array('CURLOPT_URL' => $url));

        // Create curl instance.
        $curl = curl_init();

        $this->apply_opt($curl, $options);
        if ($this->cache && $ret = $this->cache->get($this->options)) {
            return $ret;
        }

        $ret = curl_exec($curl);
        $this->info  = curl_getinfo($curl);
        $this->error = curl_error($curl);
        $this->errno = curl_errno($curl);
        // Note: $this->response and $this->rawresponse are filled by $hits->formatHeader callback.

        // In the case of redirects (which curl blindly follows), check the post-redirect URL against the blacklist entries too.
        if (intval($this->info['redirect_count']) > 0 && !$this->ignoresecurity
            && $this->securityhelper->url_is_blocked($this->info['url'])) {
            $this->reset_request_state_vars();
            $this->error = $this->securityhelper->get_blocked_url_string();
            curl_close($curl);
            return $this->error;
        }

        if ($this->emulateredirects and $this->options['CURLOPT_FOLLOWLOCATION'] and $this->info['http_code'] != 200) {
            $redirects = 0;

            while($redirects <= $this->options['CURLOPT_MAXREDIRS']) {

                if ($this->info['http_code'] == 301) {
                    // Moved Permanently - repeat the same request on new URL.

                } else if ($this->info['http_code'] == 302) {
                    // Found - the standard redirect - repeat the same request on new URL.

                } else if ($this->info['http_code'] == 303) {
                    // 303 See Other - repeat only if GET, do not bother with POSTs.
                    if (empty($this->options['CURLOPT_HTTPGET'])) {
                        break;
                    }

                } else if ($this->info['http_code'] == 307) {
                    // Temporary Redirect - must repeat using the same request type.

                } else if ($this->info['http_code'] == 308) {
                    // Permanent Redirect - must repeat using the same request type.

                } else {
                    // Some other http code means do not retry!
                    break;
                }

                $redirects++;

                $redirecturl = null;
                if (isset($this->info['redirect_url'])) {
                    if (preg_match('|^https?://|i', $this->info['redirect_url'])) {
                        $redirecturl = $this->info['redirect_url'];
                    }
                }
                if (!$redirecturl) {
                    foreach ($this->response as $k => $v) {
                        if (strtolower($k) === 'location') {
                            $redirecturl = $v;
                            break;
                        }
                    }
                    if (preg_match('|^https?://|i', $redirecturl)) {
                        // Great, this is the correct location format!

                    } else if ($redirecturl) {
                        $current = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
                        if (strpos($redirecturl, '/') === 0) {
                            // Relative to server root - just guess.
                            $pos = strpos('/', $current, 8);
                            if ($pos === false) {
                                $redirecturl = $current.$redirecturl;
                            } else {
                                $redirecturl = substr($current, 0, $pos).$redirecturl;
                            }
                        } else {
                            // Relative to current script.
                            $redirecturl = dirname($current).'/'.$redirecturl;
                        }
                    }
                }

                curl_setopt($curl, CURLOPT_URL, $redirecturl);
                $ret = curl_exec($curl);

                $this->info  = curl_getinfo($curl);
                $this->error = curl_error($curl);
                $this->errno = curl_errno($curl);

                $this->info['redirect_count'] = $redirects;

                if ($this->info['http_code'] === 200) {
                    // Finally this is what we wanted.
                    break;
                }
                if ($this->errno != CURLE_OK) {
                    // Something wrong is going on.
                    break;
                }
            }
            if ($redirects > $this->options['CURLOPT_MAXREDIRS']) {
                $this->errno = CURLE_TOO_MANY_REDIRECTS;
                $this->error = 'Maximum ('.$this->options['CURLOPT_MAXREDIRS'].') redirects followed';
            }
        }

        if ($this->cache) {
            $this->cache->set($this->options, $ret);
        }

        if ($this->debug) {
            echo '<h1>Return Data</h1>';
            var_dump($ret);
            echo '<h1>Info</h1>';
            var_dump($this->info);
            echo '<h1>Error</h1>';
            var_dump($this->error);
        }

        curl_close($curl);

        if (empty($this->error)) {
            return $ret;
        } else {
            return $this->error;
            // exception is not ajax friendly
            //throw new moodle_exception($this->error, 'curl');
        }
    }
191413
public static function simpleClient($url, $options = null, $transport = null) 
	{
		$options['version'] = 'simple';
		return self::client($url, $options, $transport);
	}
346183
public function requestRaw(string $url, string $method = 'GET', array $options = [])
    {
        return Response::buildFromPsrResponse($this->request($url, $method, $options, true));
    }
1182904
public function delete($url, array $options = [])
    {
        if ($token = $this->fetch('token')) {
            $this->bearer($token);
        }

        if (array_key_exists('delete', $this->methodHeaders)) {
            $this->addHeader('Content-Type', $this->methodHeaders['delete']['content_type']);
            $this->addHeader('Accept', $this->methodHeaders['delete']['accept']);
        }

        try {
            $response = $this->request->delete(sprintf('%s?%s', $this->getRealUrl($url, 'delete'), http_build_query(array_merge([$this->paramKey => $this->apiKey], $options))), [
                'headers' => $this->headers,
            ]);
        } catch (RequestException $e) {
            $response = $e->getResponse();
        }

        return $this->convertToSymfonyResponse($response);
    }
904340
public function addURL($url,$options=array()) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        foreach($options as $option=>$value) {
            curl_setopt($ch, $option, $value);
        }
        return $this->addCurl($ch);
    }
571228
public function makeRequest($url, $options = [], $parseResponse = true)
    {
        $headers = isset($options['headers']) ? $options['headers'] : [];
        $options['headers'] = array_merge($this->getExtraApiHeaders(), $headers);

        return parent::makeRequest($url, $options, $parseResponse);
    }
115406
private function request(string $method, string $url, array $options = [])
    {
        try {
            $response = $this->guzzleClient->request($method, $url, $options);
        } catch (RequestException $exception) {
            ResultProcessor::handleGuzzleRequestException($exception);
        }

        return ResultProcessor::processResult($url, $response);
    }
1075500
public function head($url, array $options = array())
    {
        $options[CURLOPT_NOBODY] = true;
        return $this->makeRequest($url, $options);
    }
177178
public static function post(string $url, array $options = []) : array {
		$result = [
			'success' => false,
			'error' => [],
			'data' => null
		];
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query2($options['params']));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$result['data'] = curl_exec($ch);
		if (!empty($options['json'])) {
			$result['data'] = json_decode($result['data'], true);
		}
		curl_close($ch);
		$result['success'] = true;
		return $result;
	}
331371
protected function appendUrlOptions($url, $options)
    {
        $options = array_only($options, [
            'welcome_msg',
            'timezone',
            'anonymous',
            'minimal'
        ]);

        $boolean_keys = ['minimal', 'anonymous'];
        foreach ($boolean_keys as $key) {
            if (isset($options[$key])) {
                $options[$key] = $this->booleanToString($options[$key]);
            } else {
                $options[$key] = 'true';
            }
        }

        return $url . '?' . http_build_query($options);
    }
626080
public function get($url, $params = array(), $options = array())
    {
        return $this->request($url, self::GET, $params, $options);
    }
264886
public function get($url, $data = array(), $options = array())
    {
        $query = http_build_query($data);
        if (!empty($query)) {
            $url = (strpos($url, '?')) ? $url . '&' . $query : $url . '?' . $query;
        }

        $this->request($url, false, $options);

        return $this;
    }
8680
public function post($url, array $params = null, array $options = array())
	{
		return $this->request($url, 'POST', $params, $options);
	}
1169402
public function delete($url, $data = [], array $options = [])
    {
        $options = $this->_mergeOptions($options);
        $url = $this->buildUrl($url, [], $options);

        return $this->_doRequest(Request::METHOD_DELETE, $url, $data, $options);
    }
205626
public function post($url, array $options = [])
    {
        return $this->responseBuilder->build($this->send(new Request('POST', $url), $options));
    }
1003624
public static function post($url, $headers = array(), $data = array(), $options = array()) {
		return self::request($url, $headers, $data, self::POST, $options);
	}
559763
protected function request($url, $method = self::GET, $params = array(), $options = array())
    {
        if (in_array($method, array(self::GET, self::DELETE)) && count($params)) {
            $url .= (stripos($url, '?') ? '&' : '?') . http_build_query($params);
            $params = array();
        }

        curl_setopt($this->curl, CURLOPT_HEADER, true);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($this->curl, CURLOPT_URL, $url);

        curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($this->curl, CURLOPT_TIMEOUT, 30);

        // File Upload
        if (isset($options['files']) && count($options['files']) > 0) {
            foreach ($options['files'] as $index => $file) {
                $params[$index] = $this->createCurlFile($file);
            }
            version_compare(PHP_VERSION, '5.5', '<') || curl_setopt($this->curl, CURLOPT_SAFE_UPLOAD, false);
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params);
        } elseif (isset($options['xml'])) {
            $options['headers'][] = 'content-type:text/xml';
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params['xml']);
        } elseif (isset($options['json'])) {
            $options['headers'][] = 'content-type:application/json';
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, Json::encode($params));
        } elseif (in_array($method, array(self::POST))) {
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($params));
        } else {

        }

        // Check for custom headers
        if (isset($options['headers']) && count($options['headers']))
            curl_setopt($this->curl, CURLOPT_HTTPHEADER, $options['headers']);

        // Check for basic auth
        if (isset($options['auth']['type']) && 'basic' === $options['auth']['type'])
            curl_setopt($this->curl, CURLOPT_USERPWD, $options['auth']['username'] . ':' . $options['auth']['password']);

        $response = $this->doCurl();
        // Separate headers and body
        $headerSize = $response['curl_info']['header_size'];
        $header = substr($response['response'], 0, $headerSize);
        $body = substr($response['response'], $headerSize);
        $results = array(
            'curl_info' => $response['curl_info'],
            'content_type' => $response['curl_info']['content_type'],
            'status' => $response['curl_info']['http_code'],
            'headers' => $this->splitHeaders($header),
            'data' => $body,
        );
        if ($response['response'] === false)
            $results['curl_error'] = curl_error($this->curl);
        return $results;
    }