Simple_Dom Error: file_get_contents(): stream does not support seeking in Laravel

Simple_Dom Error: file_get_contents(): stream does not support seeking in Laravel

我收到如下错误。

file_get_contents(): stream does not support seeking

我安装了 simple_dom 作曲家:

composer require sunra/php-simple-html-dom-parser

也用过这个:

use Sunra\PhpSimple\HtmlDomParser;

这是我的代码:

$weblink = "http://www.sumitomo-rd-mansion.jp/kansai/";
    function fetch_sumitomo_links($weblink)
    {
        $htmldoc = HtmlDomParser::file_get_html($weblink);
        foreach ($htmldoc->find(".areaBox a") as $a) {
            $links[]          = $a->href . '<br>';
        }
        return $links;
    }

    $items = fetch_sumitomo_links($weblink);

    print_r($items);

但是我遇到了一个错误。任何的想法? 感谢您的帮助!

答案在错误消息中。您用来读取数据的输入源不支持查找。

更具体地说,$htmldoc->find() 方法试图直接读入文件以搜索它想要的内容。但是因为你是直接通过http读取文件,不支持这个。

您的选择是先加载文件,这样 HtmlDomParser 就不必从磁盘中查找,或者如果您需要从磁盘中查找,那么它至少可以从本地数据源中读取求支持

这是问题修复者:

$url = 'http://www.sumitomo-rd-mansion.jp/kansai/';

    function fetch_sumitomo_links($url)
    {
        $htmldoc = HtmlDomParser::file_get_html($url, false, null, 0 );
        foreach ($htmldoc->find(".areaBox a") as $a) {
            $links[]          = $a->href . '<br>';
        }
        return $links;
    }

    $items = fetch_sumitomo_links($url);

    print_r($items);