asrock-memory-qvl-scraper/src/IO/Downloader.php

56 lines
1.5 KiB
PHP

<?php
namespace IO;
class Downloader
{
/** @var resource */
private $curl;
public function __construct(
private ?string $cacheDirectory = null
)
{
$this->cacheDirectory ??= dirname(__DIR__, 2) . '/var/cache';
$this->cacheDirectory = rtrim($this->cacheDirectory, DIRECTORY_SEPARATOR);
$this->curl = curl_init();
curl_setopt_array($this->curl, [
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT x.y; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FORBID_REUSE => false,
CURLOPT_AUTOREFERER => true,
]);
}
public function __destruct()
{
if (is_resource($this->curl)) {
curl_close($this->curl);
}
}
public function download(string $url, bool $addHtmlTagsToResponse = false): string
{
$file = $this->getCacheFile($url);
if (file_exists($file)) {
return file_get_contents($file);
}
curl_setopt($this->curl, CURLOPT_URL, $url);
$response = curl_exec($this->curl);
if ($addHtmlTagsToResponse) {
$response = sprintf('<!DOCTYPE html><html lang="en"><body>%s</body></html>', $response);
}
file_put_contents($file, $response);
return $response;
}
private function getCacheFile(string $url): string
{
return $this->cacheDirectory . DIRECTORY_SEPARATOR . md5($url) . '.html';
}
}