2012-01-29 24 views
0

我發現這個代碼來檢查URL上的鏈接。以前的file_get_contents數據上的PHP多個file_get_contents

<?php 
$url = "http://example.com"; 
$input = @file_get_contents($url); 
$dom = new DOMDocument(); 
$dom->strictErrorChecking = false; 
@$dom->loadHTML($input); 
$links = $dom->getElementsByTagName('a'); 
foreach($links as $link) { 
    if ($link->hasAttribute('href')) { 
     $href = $link->getAttribute('href'); 
     if (stripos($href, 'shows') !== false) { 
     echo "<p>http://example.com" . $href . "</p>\n"; 
     } 
    } 
} 

?> 

工程很好,它顯示所有包含'shows'的鏈接。 例如上面的腳本找到3個環節,所以我得到:

<p>http://example.com/shows/Link1</p> 
<p>http://example.com/shows/Link2</p> 
<p>http://example.com/shows/Link3</p> 

現在,我嘗試做的事是檢查這些網址我也只是取了一個包含「顯示」鏈接。

說實話,我是一個PHP的菜鳥,所以我不知道從哪裏開始:(

問候,巴特

+0

這個腳本是如何運行的?按需求,在計劃任務/ cron上? – alex 2012-01-29 23:15:45

+0

**注意:**如果您的深入研究第一頁你的報廢,你不存儲和檢查鏈接的欺騙,那麼你可以很容易地陷入無限循環,導致你的腳本陷入服務器和pos讓自己被阻止.. – 2012-01-29 23:21:18

回答

2

喜歡的東西:

function checklinks($url){ 
$input = @file_get_contents($url); 
$dom = new DOMDocument(); 
$dom->strictErrorChecking = false; 
@$dom->loadHTML($input); 
$links = $dom->getElementsByTagName('a'); 
foreach($links as $link) { 
    if ($link->hasAttribute('href')) { 
     $href = $link->getAttribute('href'); 
     if (stripos($href, 'shows') !== false) { 
     echo "<p>" . $url . "/" . $href . "</p>\n"; 
     checklinks($url . "/" . $href); 
     } 
    } 
} 
} 

$url = "http://example.com"; 
checklinks($url); 

讓它遞歸 - 在函數本身中再次調用函數