I recently wrote about automated link maintenance. Since the check has turned into more than just a cURL request and a look at the status code, I wrapped it up into a small Composer package.
No Symfony bundle, just a handful of classes:
use Kniebes\VerifyUrl\VerifyUrl;
$verifyUrl = new VerifyUrl();
$result = $verifyUrl->verify('https://example.com/article');
if ($result->isValid() && $result->getReplacement() === null) {
// URL is fine
}
if ($result->getReplacement() !== null) {
// Original is broken, use the snapshot from the Wayback Machine
$alternateUrl = $result->getReplacement();
}
foreach ($result->getErrors() as $error) {
// Reasons why the original URL was rejected
}
For links detected as broken, the Wayback Machine is queried for an alternative. The package waits two seconds between requests, but if you pester the service too much, you'll still get a 429 pretty quickly. That has to be handled outside the package.
There's currently a small problem with Cloudflare. If a site sits behind the Cloudflare serves and is delivering its bot challenge, the page is detected as broken and the package goes searching for a Wayback Machine alternative. I think this will need an analysis of the response headers so that such links are marked as not verifiable rather than broken.