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

1.2k views Asked by At

I'm getting a error as below.

file_get_contents(): stream does not support seeking

I installed simple_dom by a composer:

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

and used this too:

use Sunra\PhpSimple\HtmlDomParser;

This is my code:

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

But I'm getting an error. Any idea? Thanks for helping!

2

There are 2 answers

0
firefly On BEST ANSWER

This is the problem fixer:

$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);
1
Spudley On

The answer is in the error message. The input source that you are using to read the data does not support seeking.

More specifically, $htmldoc->find() method is attempting to read directly into the file to search for what it wants. But because you're reading the file directly over http, which doesn't support this.

Your options are to load the file first so that HtmlDomParser doesn't have to seek from disk or if you need to seek from disk, so it can at least read from a local data source that does support seeking.