protected function download()

in lib/php/libsdk/SDK/FileOps.php [116:166]


	protected function download(string $url, string $dest_fn = NULL) : ?string
	{/*{{{*/
		$fd = NULL;
		$retry = 0;

retry:
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, $url);

		if ($dest_fn) {
			$fd = fopen($dest_fn, "w+");
			curl_setopt($ch, CURLOPT_FILE, $fd); 
		} else {
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		}

		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_USERAGENT, Config::getSdkUserAgentName());
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

		// workaround for <https://github.com/microsoft/php-sdk-binary-tools/issues/69>
		curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

		$ret = curl_exec($ch);

		$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		if (false === $ret || 200 !== $code) {
			$err = curl_error($ch);
			curl_close($ch);
			if ($dest_fn) {
				fclose($fd);
			}
			if ($retry++ < 3) {
				goto retry;
			}
			throw new Exception($err);
		}

		curl_close($ch);

		if ($dest_fn) {
			fclose($fd);
			return NULL;
		}

		return $ret;
	}/*}}}*/