【问题标题】:Getting Fatal error using simple html dom parsing external html使用简单的 html dom 解析外部 html 出现致命错误
【发布时间】:2013-02-02 14:49:01
【问题描述】:

我正在尝试获取 TNT 包裹的最新状态(在本例中为“发货状况良好”),但无法使用简单的 html dom 解析外部 html。遇到致命错误:在第 6 行调用非对象上的成员函数 find()

<?php 
include("simple_html_dom.php");

$html = file_get_html('http://www.tnt.com/webtracker/tracking.do?&cons=323626321');

$e=$html->find('table.appTable', 1)->find('tr[valign=top]', 0)->find('td', 3);

echo $e;
?> 

即使来自http://simplehtmldom.sourceforge.net/index.htm 的示例代码也会给我同样的错误

<?php
$html = file_get_html('http://www.google.com/');
foreach($html->find('img') as $element) 
   echo $element->src . '<br>';
?>

我做错了什么?

【问题讨论】:

  • 您的服务器上是否有 file_get_contents 被阻止?!任何方式请为我们发布 var_dump($html)
  • @SoroushFalahati 获取 "bool(false)" 被阻止的 "file_get_contents" 与 "file_get_html" 有什么关系?我是否必须修改某些内容才能使其正常工作?
  • 好的,发现在 php.ini 中设置 allow_url_fopen = Off 阻止了 file_get_contents

标签: php simple-html-dom


【解决方案1】:
 file_get_html 

使用

 file_get_contents

从 url 读取 html 页面。您需要确保此功能未被阻止。在你的情况下,它似乎是

 allow_url_fopen = Off

在阻止它的 php.ini 中。

但无论如何,最好使用一个

 if ($html)

在做任何事情之前确保一切顺利。

【讨论】:

  • 谢谢,是的,我确实将 allow_url_fopen 更改为 Off,现在它可以正常工作了。
猜你喜欢
  • 2016-06-23
  • 2012-07-14
  • 1970-01-01
  • 2018-02-23
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
  • 1970-01-01
相关资源
最近更新 更多