file_get_contents(): stream does not suppo

2019-01-11 23:09发布

When was PHP behavior about this changed?

From which PHP version is it?


Warning: file_get_contents(): stream does not support seeking in /simple_html_dom.php

Warning: file_get_contents(): Failed to seek to position -1 in the stream in /simple_html_dom.php


include('parser/simple_html_dom.php');
$url = "https://en.wikipedia.org/wiki/Stack_Overflow";
$html = file_get_html($url);
if ($html !== false) {
  foreach($html->find('div#mw-content-text') as $item){
    $item->plaintext;
  }
}

7条回答
We Are One
2楼-- · 2019-01-12 00:04

Others have shared the solution, but no one has shared why. I don't know specifically why this is different between PHP 7.0 & 7.1, but the PHP.net docs for this function say:

Seeking (offset) is not supported with remote files. Attempting to seek on non-local files may work with small offsets, but this is unpredictable because it works on the buffered stream.

I can confirm that removing the offset parameter in file_get_contents on line 75 works for me and/or setting the offset to 0 in the file_get_html function on line 70 works too.

I guess that the offset parameter was never meant to be used with non local files since:

The offset where the reading starts on the original stream. Negative offsets count from the end of the stream.

Hope this helps clear up any confusion. With external sources, it makes sense to start streaming from the beginning.

查看更多
登录 后发表回答