用于测试页面上所有链接的递归函数

I am writing scraper links from all over the site, including subpages and encountered a small problem. I came up with the idea to use a recursive function because the page I want to scan has several levels. Its structure looks more or less like this:

Level 1 reference
- Second level reference
-- Third level reference
-- Third level reference
- Second level reference
-- Third level reference
-- Third level reference
-- Third level reference
--- Level four reference

It is never entirely clear whether there are more or less hidden under the tested link, hence I came up with the idea of a recursive function.

It takes a link to the main page, takes the first one and if the number of links in it is greater than one, it refers to the same function.

Unfortunately, something goes wrong and I get an empty whiteboard, how can I fix it?

function scanWebsite($url) {

        $html = file_get_contents($url);
        $dom = new DOMDocument();
        @$dom->loadHTML($html);

        $xpath = new DOMXpath($dom);
        $nodes = $xpath->query("/html/body//a");

        $output = [];

        foreach($nodes as $node) {

            $url = $node->getAttribute("href");

            if(count($nodes) > 1) {

                scanWebsite("http://samplewebsite.com" .$url);

            } else {

                if(preg_match("/\/title\/.*\//", $url)) {

                    array_push($output, $url);

                }

                continue;

            }

        }

        return $output;

    }

    echo '<pre>';
    print_r(scanWebsite("http://samplewebsite.com"));
    echo '</pre>';