<?php
/**
* Retries an HTML page retrieval operation.
*
* @param string $url The URL of the HTML page.
* @param int $maxRetries The maximum number of retries.
* @param int $retryDelay The delay between retries in seconds.
* @return string|false The HTML content if successful, false otherwise.
*/
function retryHtmlPage(string $url, int $maxRetries = 3, int $retryDelay = 5): string|false
{
$retries = 0;
while ($retries < $maxRetries) {
try {
// Fetch the HTML content.
$html = file_get_contents($url);
if ($html !== false) {
return $html; // Success!
} else {
$retries++;
if ($retries < $maxRetries) {
sleep($retryDelay);
} else {
return false; // All retries failed.
}
}
} catch (Exception $e) {
$retries++;
if ($retries < $maxRetries) {
sleep($retryDelay);
} else {
return false; // All retries failed.
}
}
}
return false; // Should not reach here if retry logic is correct.
}
/**
* Example usage:
* @param string $url
* @return string|false
*/
function getHtmlData(string $url): string|false {
$html = retryHtmlPage($url);
return $html;
}
?>
Add your comment