File: /var/www/elite/broken-link-checker.php
<?php
$sourceFile = 'list.txt';
if (empty($sourceFile)) {
echo 'Не задан файл для парсинга' . PHP_EOL;
die();
} else {
echo 'Читаем файл: ' . $sourceFile . PHP_EOL;
$row = 1;
$data = [];
if (($handle = fopen($sourceFile, "r")) !== FALSE) {
while (($line = fgetcsv($handle, 1000)) !== FALSE) {
$data[] = $line[0];
}
fclose($handle);
}
}
function getUrl($url, $followLocation = true) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $followLocation);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_exec($ch);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
$error = curl_error($ch);
curl_close($ch);
return [$info, $content, $error];
}
foreach($data as $line) {
if (!substr_count($line, "\t")) continue;
list($target, $search) = explode("\t", $line);
echo $target . ' - ';
$response = getUrl($target);
if ($response[0]['http_code'] == 200) {
if (substr_count($response[1], $search)) {
echo "Найдена ссылка " . $search . "\n";
} else {
echo "Ссылка не найдена \n";
}
} else {
echo "Страница не найдена \n";
}
// sleep(1);
}