Downloads an URL to the local disk

@access public

@param string $url URL to get
@param string $local Local filename to download to
@param array $headers Array of headers to pass to curl
@param bool|null $verifyssl

@return bool
356767
function download( $url, $local, $headers = array(), $verifyssl = null ) {
		global $argv;

		$out = fopen( $local, 'wb' );

		if( is_string( $headers ) ) curl_setopt( $this->curl_instance, CURLOPT_HTTPHEADER, array( $headers ) );
		else $this->setCurlHeaders( $headers );
		$this->setVerifySSL( $verifyssl );

		// curl_setopt($this->curl_instance, CURLOPT_FILE, $out);
		curl_setopt( $this->curl_instance, CURLOPT_HTTPGET, 1 );
		curl_setopt( $this->curl_instance, CURLOPT_POST, 0 );
		curl_setopt( $this->curl_instance, CURLOPT_URL, $url );
		curl_setopt( $this->curl_instance, CURLOPT_HEADER, 0 );

		if( ( !is_null( $argv ) && in_array( 'peachyecho', $argv ) ) || $this->echo ) {
			pecho( "DLOAD: $url\n", PECHO_NORMAL );
		}

		Hooks::runHook( 'HTTPDownload', array( &$this, &$url, &$local ) );

		fwrite( $out, $this->doCurlExecWithRetrys() );
		fclose( $out );

		return true;

	}
1216226
public static void downloadFile(final String fileUrl, final String localFile) throws IOException {
		final URL url = new URL(fileUrl);
		final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		final byte[] data = JKIOUtil.readStream(connection.getInputStream());
		JKIOUtil.writeBytesToFile(data, localFile);
	}
1160907
static public function secureUrlCached($url, $params = []) {
        if (is_array($url)) {
            $url = implode("/", $url);
        }
        $cacheKey = Cached::keymaker(__METHOD__, $url, $params);
        $cached = new Cached($cacheKey);
        if (!$cached->exists()) {
            $result = self::secureUrl($url, $params);
            syslog(LOG_INFO, "Returned " . count($result) . " rows.");
            $cached->set($result);
        }
        return $cached->get();
    }
1337358
public function doGet($url, $port = 80, $headers = NULL) {
        $retarr = array(); // Return value

        $curl_opts = array(CURLOPT_URL => $url,
            CURLOPT_PORT => $port,
            CURLOPT_POST => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_RETURNTRANSFER => true);

        if ($headers) {
            $curl_opts[CURLOPT_HTTPHEADER] = $headers;
        }

        $response = $this->doCurl($curl_opts);

        if (!empty($response)) {
            $retarr = $response;
        }

        return $retarr;
    }
1243705
public Response download(String url, File destination, String... headers) throws IOException {
		try (Response resp = response(url, headers)) {
			if (resp.isSuccessful()) {
				try (BufferedSink sink = Okio.buffer(Okio.sink(destination))) {
					resp.body().source().readAll(sink);
				}
			}
			return resp;
		}
	}
1236636
public function get($url, $query = [], $headers = [])
    {
        $headers = array_merge($this->global_headers, $headers);

        return $this->request('GET', $url, [], $query, $headers);
    }
262044
static function CurlGet($url, $data = "", $verify_cert = false, $timeout = 30)
	{
		return HttpRequest::CurlRequest("GET",$url, $data, $verify_cert, $timeout);
	}
981892
public function download($url, $target, $callback = null)
    {
        if (!$this->filesystem->fileExists($target)) {
            $this->filesystem->dirCreate(dir($target));

            $request = new Request($url, $callback ?: function ($response) {
            });

            $request->options['file'] = $target;

            $this->add($request);
        }

        return $this;
    }
23261
protected function verifyImportFromUrl($url)
    {
        $this->checkFileExtension($url);
        try {
            // need to download and extract zip file and move contents to storage
            $file = FileUtilities::importUrlFileToTemp($url);
        } catch (\Exception $ex) {
            throw new InternalServerErrorException("Failed to import file from $url. {$ex->getMessage()}");
        }

        return $file;
    }
933188
public function download($local, Filesystem $remote, array $options = array())
    {
        if (!($remote instanceof File)) {
            throw new \InvalidArgumentException(sprintf(
                "Invalid remote file given, expected instance of File, got %s",
                get_class($remote)
            ));
        }

        if (true !== is_resource($local)) {
            throw new \InvalidArgumentException(sprintf(
                "Invalid local file given. Expected resource, got %s",
                gettype($local)
            ));
        }

        if (!isset($options[ FTP::NON_BLOCKING ]) || false !== $options[ FTP::NON_BLOCKING ]) {
            throw new \InvalidArgumentException("Invalid option given. Expected false as FTP::NON_BLOCKING parameter");
        }

        $defaults = array(
            FTP::TRANSFER_MODE => FTPWrapper::BINARY,
            FTP::START_POS     => 0
        );
        $options = $options + $defaults;

        return $this->wrapper->fget($local, $remote->getRealPath(), $options[ FTP::TRANSFER_MODE ], $options[ FTP::START_POS ]);
    }
201048
private static function GetTokenFromUrl($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    $headers = curl_getinfo($ch);
    curl_close($ch);

    if ($headers['http_code'] != 200) {
      throw new OAuthException($response);
    }

    return self::GetTokenFromQueryString($response);
  }
1213987
public function get($url, array $urlParameters = [], array $headers = [], $asJSON = false)
    {
        $request = $this->prepareRequest($url, $urlParameters, $headers);

        return $this->executeRequest($request);
    }
248729
public function get($url, $params = [], $options = [], $headers = []) {
        return $this->makeCall('get', $url, $params, $options, $headers);
    }
1036466
public function downloadFile(string $url, Filesystem $filesystem, string $path): int
    {
        // Overwrite any previous/partial/failed download.
        if ($filesystem->has($path)) {
            $filesystem->delete($path);
        }

        // We store the data in PHP temporary storage.
        $tmp = fopen('php://temp', 'wb+');

        // Read from the URL
        $client   = new Client();
        $response = $client->get($url, self::GUZZLE_OPTIONS);
        $stream   = $response->getBody();

        // Download the file to temporary storage.
        while (!$stream->eof()) {
            fwrite($tmp, $stream->read(self::READ_BLOCK_SIZE));

            if ($this->timeout_service->isTimeNearlyUp()) {
                throw new InternalServerErrorException(I18N::translate('The server’s time limit has been reached.'));
            }
        }

        if (is_resource($stream)) {
            fclose($stream);
        }

        // Copy from temporary storage to the file.
        $bytes = ftell($tmp);
        rewind($tmp);
        $filesystem->writeStream($path, $tmp);
        fclose($tmp);

        return $bytes;
    }
908189
public static function loadXmlByUrl($url, $headerList = null)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($headerList) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headerList);
        }

        $content = curl_exec($ch);
        if (!$content) {

            throw new OpdsParserLoaderException('Fail to load ' . $url);
        }

        return new \SimpleXMLElement($content);
    }
939309
function supportsSSL()
    {
        $v = curl_version();
        if(is_array($v)) {
            return in_array('https', $v['protocols']);
        } elseif (is_string($v)) {
            return preg_match('/OpenSSL/i', $v);
        } else {
            return 0;
        }
    }
666417
protected function fetch_url($url, $referer = '', $timeout = null, $post_data = null, $headers = null)
    {
        // If we've been redirected too many times, end it.
        if ($this->redirects > 4) {
            $this->error = 606;
            return false;
        }

        // Set up our fake IP's.
        $ip = array(
            'REMOTE_ADDR: ' . $this->ip,
            'HTTP_X_FORWARDED_FOR: ' . $this->ip
        );

        // If we have headers to send, send them.
        if ($headers) {
            $headers = array_merge($ip, $headers);
        } else {
            $headers = $ip;
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
        curl_setopt($ch, CURLOPT_TIMEOUT, ($timeout) ? $timeout : $this->timeout);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);

        if (!empty($referer)) {
            curl_setopt($ch, CURLOPT_REFERER, $referer);
        }

        // Are we POSTing?
        if (!empty($post_data)) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }

        $result = curl_exec($ch);

        // Checking for redirects
        if (stripos($result, '<title>Object moved</title>') !== false) {
            $follow = urldecode(trim($this->find($result, '<a href="', '">')));

            if (substr($follow, 0, 1) == '/') {
                $this->redirects++;
                $result = $this->fetch_url('http://live.xbox.com' . $follow, $url);
            } else {
                $this->redirects++;
                $result = $this->fetch_url($follow, $url);
            }
        } else if (stripos($result, '<!-------Error Info') !== false) {
            // For some reason, there's an error and we cannot continue...
            $this->force_new_login();

            if ($this->logged_in) {
                $this->redirects++;
                $result = $this->fetch_url($url, $referer);
            } else {
                $this->error = 600;

                return false;
            }
        } else if (stripos($result, 'UserAcceptsNewTermsOfUse') !== false) {
            // If we get stopped to agree to a new ToU, accept it.
            $follow = 'https://live.xbox.com' . $this->find($result, '<form action="', '" method="post">');
            $token  = $this->find($result, '<input name="__RequestVerificationToken" type="hidden" value="', '" />');
            $post   = 'UserAcceptsNewTermsOfUse=true&__RequestVerificationToken=' . urlencode($token);
            $result = $this->fetch_url($follow, $url, null, $post);
            $result = $this->fetch_url($url, $referer, $timeout, $post_data, $headers);
        } else {
            $this->save_to_debug('Loaded URL: ' . $url);
        }

        if (!$result) {
            $this->error = 607;
            return false;
        }

        curl_close($ch);

        return $result;
    }