【问题标题】:Simple_Dom Error: file_get_contents(): stream does not support seeking in LaravelSimple_Dom 错误:file_get_contents():流不支持在 Laravel 中查找
【发布时间】:2019-03-27 10:41:15
【问题描述】:

我收到如下错误。

file_get_contents():流不支持搜索

我由作曲家安装了 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);

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

【问题讨论】:

  • 为什么不像其他人一样使用 DOMDocument 呢? php.net/manual/en/class.domdocument.php
  • @UdhavSarvaiya 对不起,伙计,但我不明白你所说的file_get_contents.phpsimple_html_dom.php 是什么意思,所以,这些在 Laravel 中的哪个文件夹?还是我需要安装这些,因为关于 simple_dom 安装没有记录。

标签: php laravel simpledom


【解决方案1】:

这是问题修复程序:

$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);

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 加载文件意味着,使用 DOMdocument,然后将 URL 传递给 simple_dom?
    猜你喜欢
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多