【问题标题】:Fatal error: Call to a member function find() on a non-object致命错误:在非对象上调用成员函数 find()
【发布时间】:2013-08-13 10:33:35
【问题描述】:

我是 php 新手。
我得到了

致命错误:在非对象错误上调用成员函数 find(),

我包含了 simple_html_dom.php

<?php
include 'simple_html_dom.php';
$htm = file_get_html('http://www.thatscricket.com');

$es = $htm->find('div[class=score_card_display_below_links]');

$value = $es[0]->href;

$link = "http://www.thatscricket.com/$value";

$html = file_get_html('$link');

$scr = $html->find('span');

echo "$scr";
?>

【问题讨论】:

标签: php parsing


【解决方案1】:
$html = file_get_html('$link');

这将尝试获取文字字符串'$link'(变量不会在单引号字符串中展开)。这意味着 $html 将为 null 或 false。

由于$html 不是对象,因此您不能对其调用方法。

用途:

$html = file_get_html($link);

您还应该始终检查由于失败而可能为 false 或 null 的返回类型,以便您可以优雅地失败。

【讨论】:

    【解决方案2】:

    首先检查var_dump($htm),它必须返回null

    【讨论】:

      【解决方案3】:
      $htm = file_get_html('http://www.thatscricket.com');
      

      检查var_dump($htm); 我认为它返回bool(false)

      $html = file_get_html('$link'); 
      

      应该是

      $html = file_get_html($link);
      

      【讨论】:

        【解决方案4】:

        我也遇到了同样的错误,我从这样的 php count 函数中找到了解决方案

        if( $htm ){
             $score_card_count = count($htm->find('div[class=score_card_display_below_links]'));
             $score_card_count = trim($score_card_count);
                 if( $score_card_count > 0 )
                 {
                      $es = $htm->find('div[class=score_card_display_below_links]');
                      $value = $es[0]->href;
                 }
        }
        

        我希望通过这种方法可以正常工作,我的错误已修复。

        【讨论】:

          猜你喜欢
          • 2017-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-25
          • 2012-05-30
          • 2016-08-30
          • 2015-02-06
          • 2012-05-14
          相关资源
          最近更新 更多