我正在递归调用 crawl() 函数,它获取页面的内容并回显它。当我从函数外部手动调用它时它可以工作,但是当我从函数内部递归调用它时,递归调用没有输出。我得到的唯一输出来自一个手动调用。
为什么这不起作用,我做错了什么?
<?php
error_reporting( E_ERROR );
define( "CRAWL_LIMIT_PER_DOMAIN", 50 );
$domains = array();
$urls = array();
$dom = new DOMDocument();
$matches = array();
function crawl( $domObject, $url, $matchList )
{
global $domains, $urls;
$parse = parse_url( $url );
$domains[ $parse['host'] ]++;
$urls[] = $url;
$content = file_get_contents( $url );
if ( $content === FALSE ){
return;
}
echo strip_tags($content) . "<br /><br /><br />";
array_push($matchList, 'http://www.the-irf.com/hello/hello5.html');
array_push($matchList, 'http://www.the-irf.com/hello/hello6.html');
array_push($matchList, 'http://www.the-irf.com/hello/end.html');
foreach( $matchList[0] as $crawled_url ) {
$parse = parse_url( $crawled_url );
if ( count( $domains[ $parse['host'] ] ) < CRAWL_LIMIT_PER_DOMAIN && !in_array( $crawled_url, $urls ) ) {
sleep( 1 );
crawl( $domObject, $crawled_url, $matchList );
}
}
}
crawl($dom, 'http://the-irf.com/hello/hello6.html', $matches);
?>