Skip to content

Instantly share code, notes, and snippets.

@thistehneisen
Created October 2, 2022 08:12
Show Gist options
  • Save thistehneisen/dbc818e0a93e39a8624d70597aeeb993 to your computer and use it in GitHub Desktop.
Save thistehneisen/dbc818e0a93e39a8624d70597aeeb993 to your computer and use it in GitHub Desktop.
Downloads everything from a host
<?php
$baseUrl = 'https://[domain.tld]/?item=[item]&id=[id]';
$maxItems = 1000;
$maxId = 2000;
function getUrl($url, $headersOnly = TRUE) {
$userAgent = 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = [
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_POST => FALSE,
CURLOPT_USERAGENT => $userAgent,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => $headersOnly,
CURLOPT_NOBODY => $headersOnly,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_ENCODING => '',
CURLOPT_AUTOREFERER => TRUE,
CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_TIMEOUT => 3,
CURLOPT_MAXREDIRS => 2,
];
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
if ($headersOnly) {
$data = [];
$headers = explode(PHP_EOL, $response);
foreach ($headers as $row) {
$parts = explode(':', $row);
if (count($parts) === 2) {
$data[trim($parts[0])] = trim($parts[1]);
}
}
return $data;
}
return $response;
}
for ($currentId = 1; $currentId < $maxId; $currentId++) {
for ($currentItem = 1; $currentItem <= $maxItems; $currentItem++) {
$targetUrl = str_replace(['[item]', '[id]'], [$currentItem, $currentId], $baseUrl);
$response = getUrl($targetUrl);
if (isset($response['Content-Description']) && $response['Content-Description'] === 'File Transfer') {
$fileName = preg_match('/attachment; filename="(.*)"/', $response['Content-Disposition'], $matches);
$fileName = $matches[1];
print('Downloading: ' . $fileName).PHP_EOL;
file_put_contents('data/' . $currentItem . '-' . $currentId . '-' . $fileName, getUrl($targetUrl, FALSE));
break;
} else print('Processed: ' . $targetUrl).PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment