Fetch a URL via fsockopen

@param string $url     url to fetch
@param array  $options options

@return array|false
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;
    }
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
		);
	}
384790
protected function _getScriptOutput(array $options, $url)
    {
        return $this->formatTemplate('javascriptlink', [
            'url'   => $url,
            'attrs' => $this->templater()->formatAttributes($options, ['block', 'once', 'weight', 'alias']),
        ]);
    }
358373
public static function createFromUri($url, array $options = [])
    {
        return extension_loaded('curl') ?
            self::createViaHttpClient($url, $options) : self::createViaStreamWrapper($url, $options);
    }
767298
public function fetch($url, array $params = array())
    {
        $params = array_merge(array(
            'curl' => array(),
            'fopen' => array(),
        ), $params);

        if (function_exists('curl_init') && $this->config['prefer_curl']) {
            return $this->curl($url, $params['curl']);
        }

        return $this->fileGetContents($url, $params['fopen']);
    }
405492
public function getLink($providerName, $url, array $options = array())
    {
        $this->checkProvider($providerName);

        return  $this->providers[$providerName]['provider']->getLink($url, $options);
    }
952830
protected static function fetchUrl($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['User-Agent: Mozilla/5.0 (OSX) Gecko/20030208 Netscape/7.02']);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        
        $response = curl_exec($ch);
        
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $headers = substr($response, 0, $header_size);
        $body = substr($response, $header_size);
        
        // XXX check HTTP code
        // 404 -> not found
        // 200 -> ok
        // other -> fail
        
        return [
            'code' => $httpcode,
            'headers' => $headers,
            'body' => $body
        ];
    }
285065
protected function http($url, $options = [])
    {
        $options = [
            CURLOPT_URL => $url,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_CONNECTTIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => true,
        ] + (stripos($url, "https://") !== false ? [
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_SSLVERSION =>  1// CURL_SSLVERSION_TLSv1微信官方屏蔽了ssl2和ssl3, 启用更高级的ssl
        ] : []) + $options;

        $curl = curl_init();
        curl_setopt_array($curl, $options);
        $content = curl_exec($curl);
        $status = curl_getinfo($curl);
        curl_close($curl);
        if (isset($status['http_code']) && $status['http_code'] == 200) {
            return json_decode($content, true) ?: false; // 正常加载应该是只返回json字符串
        }
        \Yii::error([
            'result' => $content,
            'status' => $status
        ],  __METHOD__);
        return false;
    }
210099
public static function loadFromUrl(string $url, array $options = [], CurlInterface $curl = null): Dom
    {
        $dom       = new Dom;
        self::$dom = $dom;
        if (is_null($curl)) {
            // use the default curl interface
            $curl = new Curl;
        }

        return $dom->loadFromUrl($url, $options, $curl);
    }
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']
        );
    }
108314
public function fetch($url, $opts)
    {
        // initialize this php abomination
        $php_errormsg = null;

        $options = array();

        $options['http'] = array();
        $options['http']['ignore_errors'] = true;

        // set custom user agent
        $options['http']['user_agent'] = 'PhealNG/' . Pheal::VERSION . ' ' . Config::getInstance()->http_user_agent;


        // set custom http timeout
        if (($http_timeout = Config::getInstance()->http_timeout) != false) {
            $options['http']['timeout'] = $http_timeout;
        }

        // ignore ssl peer verification if needed
        if (substr($url, 0, 5) == 'https') {
            $options['ssl']['verify_peer'] = Config::getInstance()->http_ssl_verifypeer;
        }

        // use post for params
        if (count($opts) && Config::getInstance()->http_post) {
            $options['http']['method'] = 'POST';
            $options['http']['content'] = http_build_query($opts, '', '&');
        } elseif (count($opts)) { // else build url parameters
            $url .= '?' . http_build_query($opts, '', '&');
        }

        // set track errors. needed for $php_errormsg
        $oldTrackErrors = ini_get('track_errors');
        ini_set('track_errors', true);

        // create context with options and request api call
        // suppress the 'warning' message which we'll catch later with $php_errormsg
        if (count($options)) {
            $context = stream_context_create($options);
            $result = file_get_contents($url, false, $context);
        } else {
            $result = @file_get_contents($url);
        }

        // check for http errors via magic $http_response_header
        $httpCode = 200;
        if (isset($http_response_header[0])) {
            list($httpVersion, $httpCode, $httpMsg) = explode(' ', $http_response_header[0], 3);
        }
        // http errors
        if (is_numeric($httpCode) && $httpCode >= 400) {
            // ccp is using error codes even if they send a valid application
            // error response now, so we have to use the content as result
            // for some of the errors. This will actually break if CCP ever uses
            // the HTTP Status for an actual transport related error.
            switch ($httpCode) {
                case 400:
                case 403:
                case 500:
                case 503:
                    return $result;
                    break;
                default:
            }
            throw new \Pheal\Exceptions\HTTPException($httpCode, $url);
        }


        // throw error
        if ($result === false) {
            $message = ($php_errormsg ? $php_errormsg : 'HTTP Request Failed');

            // set track_errors back to the old value
            ini_set('track_errors', $oldTrackErrors);

            throw new ConnectionException($message);

            // return result
        } else {
            // set track_errors back to the old value
            ini_set('track_errors', $oldTrackErrors);
            return $result;
        }

    }
116482
function canFetchURL($url)
    {
        if ($this->isHTTPS($url) && !$this->supportsSSL()) {
            Auth_OpenID::log("HTTPS URL unsupported fetching %s",
                             $url);
            return false;
        }

        if (!$this->allowedURL($url)) {
            Auth_OpenID::log("URL fetching not allowed for '%s'",
                             $url);
            return false;
        }

        return true;
    }
626080
public function get($url, $params = array(), $options = array())
    {
        return $this->request($url, self::GET, $params, $options);
    }
876457
protected function fetchEndpointForUrl($url)
    {
        $client = new Client($url);

        $body = $client->send();

        $regexp = str_replace(
            '@formats@',
            implode('|', $this->supportedFormats),
            self::LINK_REGEXP
        );

        if (!preg_match_all($regexp, $body, $matches, PREG_SET_ORDER)) {
            throw new \InvalidArgumentException('No valid oEmbed links found on page.');
        }

        foreach ($matches as $match) {
            if ($match['Format'] === $this->preferredFormat) {
                return $this->extractEndpointFromAttributes($match['Attributes']);
            }
        }

        return $this->extractEndpointFromAttributes($match['Attributes']);
    }
409611
public function fetch(array $options = [], array $modifyVars = [])
    {
        if (empty($options['overwrite'])) {
            $options['overwrite'] = true;
        }
        $overwrite = $options['overwrite'];
        unset($options['overwrite']);

        $matches = $css = $less = [];
        preg_match_all('@(<link[^>]+>)@', $this->_View->fetch('css'), $matches);

        if (empty($matches)) {
            return null;
        }

        $matches = array_shift($matches);

        foreach ($matches as $stylesheet) {
            if (strpos($stylesheet, 'rel="stylesheet/less"') !== false) {
                $match = [];
                preg_match('@href="([^"]+)"@', $stylesheet, $match);
                $file = rtrim(array_pop($match), '?');
                array_push($less, $this->less($file, $options, $modifyVars));
                continue;
            }
            array_push($css, $stylesheet);
        }

        if ($overwrite) {
            $this->_View->Blocks->set('css', join($css));
        }

        return join($less);
    }
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;
    }
510318
public function makeRequest($url, $options = [], $parseResponse = true)
    {
        return $this->request($url, $options, $parseResponse, function ($url, $method, $headers, $data) {
            return $this->getHttpClient()->retrieveResponse($url, $data, $headers, $method);
        });
    }