我有一个包含 1000 多个外部链接的 php 数字数组。你能帮我找出一个 PHP 或 JS 函数来循环遍历每个链接并检查它是否工作吗?创建一个数组并包含不再工作的链接。
现在我正在使用这段代码:
$links = array(
'http://google.com',
'http://example.com',
'http://awkrlalrno1in01n2rn12r12r.com',
'http://112om1om1om.ru'
);
foreach($links as $link) {
if($file_headers = @get_headers($link)) {
if(strpos($file_headers[0],'404') !== false) {
$toDeleteLinks[] = $link;
}
}elseif($handle = curl_init($link)) {
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
//Get the HTML or whatever is linked in $url.
$response = curl_exec($handle);
//Check for 404 (file not found).
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 0) {
$toDeleteLinks[] = $link;
}
curl_close($handle);
}
}
if(isset($toDeleteLinks)) {
echo '<pre>';
print_r($toDeleteLinks);
echo '</pre>';
}
但它给出了 500 Internal Server Error .. 似乎服务器无法处理这么多请求,因为我尝试创建一个包含 4 个链接、2 个正确链接、2 个不正确链接的数组,并且该函数具有魅力。
我即将把我的耳朵从我的脑海中拉出来,所以请帮助 :D 在此先感谢!