file_get_contents(): 流不支持查找 / PHP 什么时候改变了这个行为?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42685814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:40:49  来源:igfitidea点击:

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

phpsimple-html-dom

提问by re1

When was PHP behavior about this changed?

PHP 对此的行为何时发生变化?

From which PHP version is it?

它来自哪个 PHP 版本?



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

警告:file_get_contents():流不支持在 /simple_html_dom.php 中查找

警告:file_get_contents():未能在 /simple_html_dom.php 的流中寻找位置 -1



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;
  }
}

采纳答案by le_m

See file_get_contents(): stream does not support seeking PHP

file_get_contents():流不支持搜索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.

在使用file_get_html. 它应该在本地主机上正常工作。

回答by Rmj86

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.phpfile by removing the offset reference (didn't cause any further problems for me).

当我将它从一个系统移到另一个系统时,我的页面上遇到了同样的问题,我能够simple_html_dom.php通过删除偏移引用来更改文件(没有对我造成任何进一步的问题)。

On line 75 of simple_html_dom.php:

在第 75 行simple_html_dom.php

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

I removed the reference to $offset:

我删除了对$offset以下内容的引用:

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

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

不,我的页面工作正常。对它破坏的任何其他东西不承担任何责任!:)

回答by Neibce

Change

改变

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

to

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

in simple_html_dom.php

在 simple_html_dom.php 中

回答by Chuck Le Butt

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.

问题是简单 HTML DOM 使用的默认偏移量是“-1”,当您希望它为“0”时。幸运的是,它接受它作为参数,这意味着您可以轻松更改它,而无需更改 Simple HTML DOM 源。

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

注意:此兼容性问题已在 v1.7+ 中修复

回答by Crazycoolcam

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 functionsay:

其他人已经分享了解决方案,但没有人分享原因。我不知道为什么这在 PHP 7.0 和 7.1 之间有所不同,但是这个函数PHP.net 文档说:

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_contentson line 75 works for me and/or setting the offset to 0 in the file_get_htmlfunction on line 70 works too.

我可以确认删除第file_get_contents75 行中的偏移参数对我file_get_html有用和/或在第 70 行的函数中将偏移量设置为 0也有效。

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.

希望这有助于消除任何混淆。对于外部资源,从头开始流式传输是有意义的。

回答by Carlos Diaz

Set $offset = 0

设置 $offset = 0

That is working!

那是有效的!