@param string       $url
@param array|string $options

@return bool
174812
public static function match($url, $options)
    {
        if (!is_array($options)) {
            $tmpString = $options;
            $options = null;
            $options['host'] = $tmpString;
        }

        // build the rules
        $rules = '';
        foreach ($options as $key => $value) {
            switch (Str::lower($key)) {
                case 'scheme':
                    $rules .= "|scheme:${value}";
                    break;
                case 'user':
                    $rules .= "|user:${value}";
                    break;
                case 'pass':
                    $rules .= "|pass:${value}";
                    break;
                case 'host':
                    $rules .= "|host:${value}";
                    break;
                case 'subdomain':
                    $rules .= "|subdomain:${value}";
                    break;
                case 'domain':
                    $rules .= "|domain:${value}";
                    break;
                case 'tld':
                    $rules .= "|tld:${value}";
                    break;
                case 'port':
                    $rules .= "|port:${value}";
                    break;
                case 'path':
                    $rules .= "|path:${value}";
                    break;
                case 'query':
                    $rules .= "|query:${value}";
                    break;
                case 'fragment':
                    $rules .= "|fragment:${value}";
                    break;
            }
        }

        $validate = Validator::make(['url' => $url], [
            'url' => 'required|url'.$rules,
        ]);

        return $validate->passes();
    }
1156013
protected function _curl($url,$options) {
		$curl=curl_init($url);
		curl_setopt($curl,CURLOPT_FOLLOWLOCATION,
			$options['follow_location']);
		curl_setopt($curl,CURLOPT_MAXREDIRS,
			$options['max_redirects']);
		curl_setopt($curl,CURLOPT_CUSTOMREQUEST,$options['method']);
		if (isset($options['header']))
			curl_setopt($curl,CURLOPT_HTTPHEADER,$options['header']);
		if (isset($options['content']))
			curl_setopt($curl,CURLOPT_POSTFIELDS,$options['content']);
		curl_setopt($curl,CURLOPT_ENCODING,'gzip,deflate');
		$timeout=isset($options['timeout'])?
			$options['timeout']:
			ini_get('default_socket_timeout');
		curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,$timeout);
		curl_setopt($curl,CURLOPT_TIMEOUT,$timeout);
		$headers=array();
		curl_setopt($curl,CURLOPT_HEADERFUNCTION,
			// Callback for response headers
			function($curl,$line) use(&$headers) {
				if ($trim=trim($line))
					$headers[]=$trim;
				return strlen($line);
			}
		);
		curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
		ob_start();
		curl_exec($curl);
		curl_close($curl);
		$body=ob_get_clean();
		return array(
			'body'=>$body,
			'headers'=>$headers,
			'engine'=>'cURL',
			'cached'=>FALSE
		);
	}
1066567
public function fetch($url, $options = array())
    {
        $this->debug->groupCollapsed(__METHOD__);
        $optionsDefault = $this->getOptionsDefault();
        $options = $this->getOptions($optionsDefault, $options);
        $urlParts = Html::parseUrl($url);
        $useCurl = $urlParts['scheme'] == 'https' || \array_diff_key($options, $optionsDefault);
        $return = $useCurl
            ? $this->fetchCurl($url, $options)
            : $this->fetchFsock($url, $options);
        #$this->debug->log('return',$return);
        $this->debug->groupEnd();
        return $return;
    }
1025859
protected function exec(Url $url, array $options)
    {
        $connection = curl_init((string) $url);
        curl_setopt_array($connection, $options);

        $curl = new CurlResult($connection);

        //Get only text responses
        $curl->onHeader(function ($name, $value, $data) {
            if ($name === 'content-type') {
                $data->isBinary = !preg_match('/(text|html|json)/', strtolower($value));
            }
        });

        $curl->onBody(function ($string, stdClass $data) {
            return empty($data->isBinary);
        });

        curl_exec($connection);

        $result = $curl->getResult();

        curl_close($connection);

        return $this->responses[] = new Response(
            $url,
            Url::create($result['url']),
            $result['statusCode'],
            $result['contentType'],
            $result['content'],
            $result['headers'],
            $result['info']
        );
    }
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;
    }
169477
private function request($url, array $options)
    {
        $response = $this->client->request('POST', $url, $options);

        $body = $response->getBody();
        if ($response->getStatusCode() != 200
                || !isset($body)) {
            throw new \Exception('Failed request to ' . $this->endpoint . '.');
        }

        return $response;
    }
815141
protected function _socket($url,$options) {
		$eol="\r\n";
		$headers=array();
		$body='';
		$parts=parse_url($url);
		$empty=empty($parts['port']);
		if ($parts['scheme']=='https') {
			$parts['host']='ssl://'.$parts['host'];
			if ($empty)
				$parts['port']=443;
		}
		elseif ($empty)
			$parts['port']=80;
		if (empty($parts['path']))
			$parts['path']='/';
		if (empty($parts['query']))
			$parts['query']='';
		$socket=@fsockopen($parts['host'],$parts['port']);
		if (!$socket)
			return FALSE;
		stream_set_blocking($socket,TRUE);
		stream_set_timeout($socket,$options['timeout']);
		fputs($socket,$options['method'].' '.$parts['path'].
			($parts['query']?('?'.$parts['query']):'').' HTTP/1.0'.$eol
		);
		fputs($socket,implode($eol,$options['header']).$eol.$eol);
		if (isset($options['content']))
			fputs($socket,$options['content'].$eol);
		// Get response
		$content='';
		while (!feof($socket) &&
			($info=stream_get_meta_data($socket)) &&
			!$info['timed_out'] && !connection_aborted() &&
			$str=fgets($socket,4096))
			$content.=$str;
		fclose($socket);
		$html=explode($eol.$eol,$content,2);
		$body=isset($html[1])?$html[1]:'';
		$headers=array_merge($headers,$current=explode($eol,$html[0]));
		$match=NULL;
		foreach ($current as $header)
			if (preg_match('/Content-Encoding: (.+)/',$header,$match))
				break;
		if ($match)
			switch ($match[1]) {
				case 'gzip':
					$body=gzdecode($body);
					break;
				case 'deflate':
					$body=gzuncompress($body);
					break;
			}
		if ($options['follow_location'] &&
			preg_match('/Location: (.+?)'.preg_quote($eol).'/',
			$html[0],$loc)) {
			$options['max_redirects']--;
			return $this->request($loc[1],$options);
		}
		return array(
			'body'=>$body,
			'headers'=>$headers,
			'engine'=>'socket',
			'cached'=>FALSE
		);
	}
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);
    }
495039
protected function fetchFsock($url, $options = array())
    {
        $this->debug->groupCollapsed(__METHOD__);
        $return = false;
        $urlParts = Html::parseUrl($url);
        // $this->debug->log('urlParts', $urlParts);
        // $this->debug->log('options', $options);
        if (!empty($options['proxy']) && !\in_array($urlParts['host'], array('127.0.0.1','localhost'))) {
            $this->debug->log('proxy', $options['proxy']);
            $request = 'GET '.$url.' HTTP/1.0'."\r\n";
            $proxyParts = \parse_url($options['proxy']);
            $pointer = \fsockopen($proxyParts['host'], $proxyParts['port'], $errno, $errstr, $options['timeout']);
        } else {
            $uri = $urlParts['path'].( !empty($urlParts['query']) ? '?'.$urlParts['query'] : '' );
            $request = 'GET '.$uri.' HTTP/1.0'."\r\n";
            $pointer = \fsockopen($urlParts['host'], $urlParts['port'], $errno, $errstr, $options['timeout']);
        }
        if ($pointer) {
            $host = $urlParts['host'].( $urlParts['port'] != '80' ? ':'.$urlParts['port'] : '' );
            $return = '';
            $request .= ''
                .'Host: '.$host."\r\n"
                .'Referer: '.Html::getSelfUrl(array(), array('fullUrl'=>true,'chars'=>false))."\r\n"
                .( !empty($urlParts['user'])
                    ? 'Authorization: Basic '.\base64_encode($urlParts['user'].':'.$urlParts['pass'])."\r\n"
                    : ''
                )
                ."\r\n";
            $this->debug->log('request', $request);
            \fputs($pointer, $request);
            while (!\feof($pointer)) {
                $return .= \fread($pointer, 4096);
            }
            \fclose($pointer);
            $this->debug->log('return', $return);
            $header_start = 0;
            while (true) {
                $header_end     = \strpos($return, "\r\n\r\n", $header_start);
                $header_length  = $header_end-$header_start;
                $headers        = \substr($return, $header_start, $header_length);
                $header_start   = $header_end+4;
                if (!\preg_match('/^HTTP.+Continue/i', $headers)) {
                    break;
                }
            }
            $this->debug->log('headers', $headers);
            $parsedHeaders = Net::parseHeaders($headers);
            $return = array(
                'headers'   => $parsedHeaders,
                'body'      => \substr($return, $header_end+4),
                'cookies'   => $this->getCookies($parsedHeaders),
            );
            $this->fetchResponse = $return;
            if (isset($return['headers']['Location'])) {
                $this->debug->log('headers', $return['headers']);
                $location = Html::getAbsUrl($urlParts, $return['headers']['Location']);
                $return = $this->fetch($location, $options);
            } elseif ($return['headers']['Return-Code'] != '200') {
                $this->debug->log('return[headers]', $return['headers']);
                if (isset($return['headers']['Proxy-Authenticate'])
                    && ( $return['headers']['Proxy-Authenticate'] == 'NTLM' || \in_array('NTLM', $return['headers']['Proxy-Authenticate']))
                ) {
                    $return = $this->fetchCurl($url, $options);
                    $return['headers']['Current-Location'] = $url;
                } else {
                    $this->debug->log('return was', $return);
                    $return = false;
                }
            }
        } else {
            $this->debug->warn('Error ('.$errno.') : ', $errstr);
        }
        $this->debug->groupEnd();
        return $return;
    }
485236
public function post($url, $params = '', $options = array()) {
        return parent::post($url, format_postdata_for_curlcall($params), $options);
    }
905129
public static function get(string $url, array $options = []) : array {
		$result = [
			'success' => false,
			'error' => [],
			'data' => null
		];
		if (strpos($url, '?') !== false) {
			$url.= '&';
		} else {
			$url.= '?';
		}
		$url.= http_build_query2($options['params']);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		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;
	}
1301300
public function options($url, $data = [], array $options = [])
    {
        $options = $this->_mergeOptions($options);
        $url = $this->buildUrl($url, [], $options);

        return $this->_doRequest(Request::METHOD_OPTIONS, $url, $data, $options);
    }
1300280
public function send(string $url, array $options = []): array
    {
        $response = $this->client->get($url, $options);

        $result = \GuzzleHttp\json_decode($response->getBody()->getContents(), true);
        if (!empty($result['Command']) && $result['Command'] === 'Unknown') {
            throw new UnknownCommandException('command is unknown');
        }

        return $result;
    }
815999
private function _onDisabled($url,$options ){
		$onDisabled = $options['onDisabled'];
		unset($options['onDisabled']);
		
		
		if ($onDisabled === true) {
			$url = false;
			$options = $this->addClass($options,'disabled');
			$exit = true;
		}
	
		return array($url, $options);	
	}
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');
        }
    }
360934
private function sendRequest($method, $url, array $options = [])
    {
        $response = $this->httpClient->request((string)$method, $url, $options);

        return json_decode($response->getBody(), true);
    }
626080
public function get($url, $params = array(), $options = array())
    {
        return $this->request($url, self::GET, $params, $options);
    }