Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents(): stream does not support seeking / When was PHP behavior about this changed?

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;
  }
}
like image 853
re1 Avatar asked Mar 09 '17 02:03

re1


4 Answers

I had the same issue on my page when I moved it from one system to another, I was able to change the simple_html_dom.php file by removing the offset reference (didn't cause any further problems for me).

On line 75 of simple_html_dom.php:

$contents = file_get_contents($url, $use_include_path, $context, $offset);

I removed the reference to $offset:

$contents = file_get_contents($url, $use_include_path, $context);

No my page works fine. Not taking liability for anything else it breaks! :)

like image 134
Rmj86 Avatar answered Oct 01 '22 01:10

Rmj86


Change

function file_get_html(..., $offset = -1,...)

to

function file_get_html(..., $offset = 0,...)

in simple_html_dom.php

like image 40
Neibce Avatar answered Sep 29 '22 01:09

Neibce


You don't need to edit the vendor files. Just change your requests from:

$html = HtmlDomParser::file_get_html( "https://www.google.com/");

to:

$html = HtmlDomParser::file_get_html( "https://www.google.com/", false, null, 0 );

The problem is that the default offset used by Simple HTML DOM is "-1" when you want it to be "0". Luckily it accepts it as a parameter, which means you can change it easily without needing to change the Simple HTML DOM source.

Note: This compatibility issue was fixed in v1.7+

like image 20
Chuck Le Butt Avatar answered Sep 30 '22 01:09

Chuck Le Butt


See file_get_contents(): stream does not support seeking PHP

You are working with a remote file. Seeking is only supported for local files.

You probably need to copy the file to your local file system before using file_get_html. It should work fine on localhost.

like image 3
le_m Avatar answered Oct 02 '22 01:10

le_m