2

我是 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";
?>
4

4 回答 4

2
$html = file_get_html('$link');

This will try to get the literal string '$link' (variables aren't expanded inside single quoted strings). Which means $html will be null or false.

Since $html isn't an object you can't call methods on it.

Use:

$html = file_get_html($link);

You should also always check return types that may be false or null due to failure so that you can fail gracefully.

于 2013-08-13T10:37:08.210 回答
0
$htm = file_get_html('http://www.thatscricket.com');

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

$html = file_get_html('$link'); 

应该

$html = file_get_html($link);
于 2013-08-13T10:36:05.360 回答
0

check the var_dump($htm) first, it must be returning null

于 2013-08-13T10:37:28.007 回答
0

我也遇到了同样的错误,我从这样的 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;
         }
}

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

于 2014-11-07T07:40:24.000 回答