<?php
declare(strict_types=1);
class Download
{
/**
* @var false|resource
*/
private static $curlHandle = false;
/**
* Common initialization for download and downloadToFile
* Return file handle to header file
* @return false|resource
*/
private static function initCurl(string $url, int $timeout)
{
if (self::$curlHandle === false) {
self::$curlHandle = curl_init();
if (self::$curlHandle === false) {
ErrorHandler::traceError('Could not initialize cURL');
}
curl_setopt(self::$curlHandle, CURLOPT_CONNECTTIMEOUT, ceil($timeout / 2));
curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT, $timeout);
curl_setopt(self::$curlHandle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt(self::$curlHandle, CURLOPT_AUTOREFERER, true);
curl_setopt(self::$curlHandle, CURLOPT_MAXREDIRS, 6);
}
curl_setopt(self::$curlHandle, CURLOPT_URL, $url);
return self::$curlHandle;
}
/**
* Download file, obey given timeout in seconds
* Return data on success, false on failure
*/
public static function asString(string $url, int $timeout, ?int &$code)
{
$ch = self::initCurl($url, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
return $data;
}
/**
* POST-Download file, obey given timeout in seconds
* Return data on success, false on failure
* @param string $url URL to fetch
* @param array|false $params POST params to set in body, list of key-value-pairs
* @param int $timeout timeout in seconds
* @param ?int $code HTTP response code, or 999 on error
* @return string|false
*/
public static function asStringPost(string $url, $params, int $timeout, ?int &$code)
{
$string = '';
if (is_array($params)) {
foreach ($params as $k => $v) {
if (!empty($string)) {
$string .= '&';
}
$string .= $k . '=' . urlencode($v);
}
}
$ch = self::initCurl($url, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
$data = curl_exec($ch);
$code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
return $data;
}
/**
* Download a file from a URL to file.
*
* @param string $target destination path to download file to
* @param string $url URL of file to download
* @param int $timeout timeout in seconds
* @param ?int $code HTTP status code passed out by reference
*/
public static function toFile(string $target, string $url, int $timeout, ?int &$code): bool
{
$fh = fopen($target, 'wb');
if ($fh === false)
ErrorHandler::traceError("Could not open $target for writing.");
$ch = self::initCurl($url, $timeout);
curl_setopt($ch, CURLOPT_FILE, $fh);
$res = curl_exec($ch);
$code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
fclose($fh);
if ($res === false) {
@unlink($target);
return false;
}
return true;
}
}