在非对象IN Simple_html_Dom上调用成员函数find()

时间:2012-01-22 11:42:58

标签: php parsing dom

我这堂课有一个奇怪的问题。 如果我使用像test.html这样的本地文件,代码可以正常工作而不会出现任何错误,但如果我使用像google.com这样的远程页面,我会收到此错误:

Call to a member function find() on a non-object

我不明白为什么会这样,也不能解决它

这是代码

<?php
include('simple_html_dom.php');
$id="hawkiq"; // any PSN id like : ashiqiraqi,duck360...etc
$url='http://www.yourgamercards.net/profile/'.$id; // url for profile page.

$html = new simple_html_dom();
$html->load_file($url);

$img = $html->find('img[alt='.$id.']',0); // Get User image .

foreach($html->find('span.about1me') as $e)
    $about= $e->outertext; // Get comment field .


foreach($html->find('td.tlevel') as $e)
    $s= $e->innertext;
    $level=strip_tags($s); // Get level 


foreach($html->find('td.platbg') as $e)
   $s= $e->innertext ;
    $platinum=strip_tags($s); // Get Platinum Trophies number .


foreach($html->find('td.goldbg') as $e)
    $s= $e->innertext ;
    $gold=strip_tags($s); // Get Gold Trophies number .


foreach($html->find('td.silverbg') as $e)
    $s= $e->innertext ;
    $silver=strip_tags($s); // Get Silver Trophies number .


foreach($html->find('td.bronzebg') as $e)
    $s= $e->innertext ;
    $bronze=strip_tags($s); // Get Bronze Trophies number .


foreach($html->find('td.totaltrophies') as $e)
    $s= $e->innertext ;
    $total=strip_tags($s); // Get Total trophies

    // Under construction :) .

// foreach($html->find('p.statsbottomcard') as $e)
    // $s= $e->innertext ;
    // $s=strip_tags($s);
    // echo $s;


    // Table for showing the results .
    echo '<table width="71%" border="1" align="center" cellpadding="1" cellspacing="0" bordercolor="#0066CC">
  <tr>
    <td width="4%" bgcolor="#E1E1E1"><div align="center"><img src="rank.png" width="16" height="16" /></div></td>
    <td width="3%" bgcolor="#E1E1E1"><div align="center"><img src="unk.png" width="16" height="11" /></div></td>
    <td width="4%" bgcolor="#E1E1E1"><div align="center"><img src="60c562b0cf2ada61af5a7a0c48cf7e55.png" width="25" height="25" /></div></td>
    <td width="33%" bgcolor="#E1E1E1"><div align="center" class="style1">Username ( PSN ID ) </div></td>
    <td width="8%" bgcolor="#E1E1E1"><div align="center"><img src="25_level.png" width="25" height="25" /></div></td>
    <td width="5%" bgcolor="#FEBE7D"><div align="center"><img src="25_bronze.png" width="25" height="25" /></div></td>
    <td width="5%" bgcolor="#C3C3C3"><div align="center"><img src="25_silver.png" width="25" height="25" /></div></td>
    <td width="6%" bgcolor="#F2BF5D"><div align="center"><img src="25_gold.png" width="25" height="25" /></div></td>
    <td width="5%" bgcolor="#6D82AF"><div align="center"><img src="25_platinum.png" width="25" height="26" /></div></td>
    <td width="12%" bgcolor="#E1E1E1"><div align="center"><img src="total.png" width="35" height="30" /></div></td>
    <td width="15%" bgcolor="#E1E1E1"><div align="center"><img src="points.png" width="16" height="16" /></div></td>
  </tr>
  <tr>
    <td bgcolor="#F2F2F2" class="style1"><div align="center"><strong>1</strong></div></td>
    <td bgcolor="#F2F2F2"><div align="center"><img src="kw.png" width="16" height="11" /></div></td>
    <td bgcolor="#F2F2F2"><div align="center">'.$img.'</div></td>
    <td bgcolor="#F2F2F2" class="style1"><div align="center" class="style4">
      <div align="center"><a href="#" class="style5">'.$id.'</a><br>'.$about.'</div>
    </div></td>
    <td bgcolor="#F2F2F2" class="style1"><div align="center">'.$level.'</div></td>
    <td bgcolor="#FEBE7D" class="style1"><div align="center">'.$platinum.'</div></td>
    <td bgcolor="#C3C3C3" class="style1"><div align="center">'.$gold.'</div></td>
    <td bgcolor="#F2BF5D" class="style1"><div align="center">'.$silver.'</div></td>
    <td bgcolor="#6D82AF" class="style1"><div align="center">'.$bronze.'</div></td>
    <td bgcolor="#F2F2F2" class="style1"><div align="center">'.$total.'</div></td>
    <td bgcolor="#F2F2F2" class="style1"><div align="center">not implemented</div></td>
  </tr>';
?>

2 个答案:

答案 0 :(得分:1)

该错误基本上意味着你试图在$ html-&gt; find上调用find()但是$ html不是对象

 <?php
include('simple_html_dom.php');
$id="hawkiq"; // any PSN id like : ashiqiraqi,duck360...etc
$url='http://www.yourgamercards.net/profile/'.$id; // url for profile page.

$html = new simple_html_dom();
  //here $html is now object if this will not work use $html = file_get_html($url); this will work for you surely
$html = $html->load_file($url);

$img = $html->find('img[alt='.$id.']',0); // Get User image .

foreach($html->find('span.about1me') as $e)
$about= $e->outertext; // Get comment field .


foreach($html->find('td.tlevel') as $e)
    $s= $e->innertext;
$level=strip_tags($s); // Get level 


foreach($html->find('td.platbg') as $e)
   $s= $e->innertext ;
    $platinum=strip_tags($s); // Get Platinum Trophies number .


foreach($html->find('td.goldbg') as $e)
    $s= $e->innertext ;
    $gold=strip_tags($s); // Get Gold Trophies number .


foreach($html->find('td.silverbg') as $e)
    $s= $e->innertext ;
    $silver=strip_tags($s); // Get Silver Trophies number .


foreach($html->find('td.bronzebg') as $e)
    $s= $e->innertext ;
    $bronze=strip_tags($s); // Get Bronze Trophies number .


foreach($html->find('td.totaltrophies') as $e)
    $s= $e->innertext ;
    $total=strip_tags($s); // Get Total trophies

    // Under construction :) .

 // foreach($html->find('p.statsbottomcard') as $e)
    // $s= $e->innertext ;
    // $s=strip_tags($s);
    // echo $s;


    // Table for showing the results .
    echo '<table width="71%" border="1" align="center" cellpadding="1" cellspacing="0" bordercolor="#0066CC">
  <tr>
    <td width="4%" bgcolor="#E1E1E1"><div align="center"><img src="rank.png" width="16"   height="16" /></div></td>
<td width="3%" bgcolor="#E1E1E1"><div align="center"><img src="unk.png" width="16" height="11" /></div></td>
<td width="4%" bgcolor="#E1E1E1"><div align="center"><img src="60c562b0cf2ada61af5a7a0c48cf7e55.png" width="25" height="25" /></div></td>
<td width="33%" bgcolor="#E1E1E1"><div align="center" class="style1">Username ( PSN ID ) </div></td>
<td width="8%" bgcolor="#E1E1E1"><div align="center"><img src="25_level.png" width="25" height="25" /></div></td>
<td width="5%" bgcolor="#FEBE7D"><div align="center"><img src="25_bronze.png" width="25" height="25" /></div></td>
<td width="5%" bgcolor="#C3C3C3"><div align="center"><img src="25_silver.png" width="25" height="25" /></div></td>
<td width="6%" bgcolor="#F2BF5D"><div align="center"><img src="25_gold.png" width="25" height="25" /></div></td>
<td width="5%" bgcolor="#6D82AF"><div align="center"><img src="25_platinum.png" width="25" height="26" /></div></td>
<td width="12%" bgcolor="#E1E1E1"><div align="center"><img src="total.png" width="35" height="30" /></div></td>
<td width="15%" bgcolor="#E1E1E1"><div align="center"><img src="points.png" width="16" height="16" /></div></td>
  </tr>
  <tr>
     <td bgcolor="#F2F2F2" class="style1"><div align="center"><strong>1</strong></div></td>
    <td bgcolor="#F2F2F2"><div align="center"><img src="kw.png" width="16" height="11" /></div></td>
<td bgcolor="#F2F2F2"><div align="center">'.$img.'</div></td>
<td bgcolor="#F2F2F2" class="style1"><div align="center" class="style4">
  <div align="center"><a href="#" class="style5">'.$id.'</a><br>'.$about.'</div>
</div></td>
<td bgcolor="#F2F2F2" class="style1"><div align="center">'.$level.'</div></td>
<td bgcolor="#FEBE7D" class="style1"><div align="center">'.$platinum.'</div></td>
<td bgcolor="#C3C3C3" class="style1"><div align="center">'.$gold.'</div></td>
<td bgcolor="#F2BF5D" class="style1"><div align="center">'.$silver.'</div></td>
<td bgcolor="#6D82AF" class="style1"><div align="center">'.$bronze.'</div></td>
<td bgcolor="#F2F2F2" class="style1"><div align="center">'.$total.'</div></td>
<td bgcolor="#F2F2F2" class="style1"><div align="center">not implemented</div></td>
  </tr>';
  ?>

答案 1 :(得分:0)

您无需将load_file()方法的返回值赋给变量。以下代码应该可以正常工作

$html = new simple_html_dom();
$html->load_file($url); // No need to assign the return value to a variable, instead just use $html as you normally would.
// Rest of the code here...