我正在尝试在启动时编写自定义Laravel函数,但出现错误

时间:2018-11-20 20:24:44

标签: php laravel

因此,我试图在App Service Provider中编写以下功能,但出现错误:

我的代码是:

public function boot()
{
    $homepage = 'https://example.com';
    $already_crawled = [];
    $crawling = [];

    function follow_links($url)
    {
        global $already_crawled;
        global $crawling;

        $doc = new \DOMDocument();
        $doc->loadHTML(file_get_contents($url));

        $linklist = $doc->getElementsByTagName('a');

        foreach ($linklist as $link) {
            $l = $link->getAttribute("href");
            $full_link = 'https://example.com' . $l;

            if (!in_array($full_link, $already_crawled)) {
                $already_crawled[] = $full_link;
                $crawling[] = $full_link;
                Log::info($full_link . PHP_EOL);
            }
        }

        array_shift($crawling);
        foreach ($crawling as $link) {
            follow_links($link);
        }
    }

    follow_links($homepage);
}

因此,使用此代码,我会收到类似以下错误:

in_array()预期参数2为数组,给定null

我应该怎么做才能毫无问题地运行它?

1 个答案:

答案 0 :(得分:1)

您的boot函数中的变量不是global,因此follow_links函数的全局变量是一组完全独立的变量。基本上,Laravel中的任何地方都永远不要使用关键字global

由于范围问题,当您初次尝试将其馈送到$already_crawled时,is_array是不确定的。使用类属性,然后使用$this来访问它们。最重要的是,我删除了怪异的“函数中函数”构造:

protected $already_crawled;
protected $crawling;
protected $homepage;

public function boot()
{
    $this->homepage = 'https://example.com';
    $this->already_crawled = [];
    $this->crawling = [];

    $this->follow_links($this->homepage);
}

protected function follow_links($url)
{
    $doc = new \DOMDocument();
    $doc->loadHTML(file_get_contents($url));

    $linklist = $doc->getElementsByTagName('a');

    foreach ($linklist as $link) {
        $l = $link->getAttribute("href");
        $full_link = 'https://example.com' . $l;

        if (!in_array($full_link, $this->already_crawled)) {
            $this->already_crawled[] = $full_link;
            $this->crawling[] = $full_link;
            Log::info($full_link . PHP_EOL);
        }
    }

    array_shift($this->crawling);
    foreach ($this->crawling as $link) {
        $this->follow_links($link);
    }
}

旁注:您几乎可以肯定不需要在您的服务提供商中使用。它将在您的应用所服务的每个页面浏览量上进行HTTP file_get_contents调用。这将大大降低您的应用速度。