【问题标题】:"Trying to get property of non-object" error PHP“试图获取非对象的属性”错误 PHP
【发布时间】:2014-05-08 17:11:24
【问题描述】:

我正在尝试从谷歌搜索接收价格数据,这段代码给了我一个“尝试获取非对象的属性”

      <?php
      $html = file_get_contents('http://www.google.com/#q=ps3&tbm=shop'); //get the html returned from the following url
      $glist = array();
      $gdoc = new DOMDocument();
      libxml_use_internal_errors(TRUE); //disable libxml errors
      if(!empty($html)){ //if any html is actually returned

         $gdoc->loadHTML($html);
         libxml_clear_errors(); //remove errors for yucky html 
         $gxpath = new DOMXPath($gdoc);
         $name = $gxpath->query('b')->item(0)->nodeValue;
      }
       echo "<pre>";
       print_r($glist);
       echo "</pre>";
      ?>

任何帮助都会很好!

【问题讨论】:

    标签: php html dom curl xhtml


    【解决方案1】:

    如果没有更多信息,可能很难确定它的来源。

    很可能来自这条线:

    $name = $gxpath->query('b')->item(0)->nodeValue;
    

    可能是查询、项目或 nodeValue 不存在。尝试打印出 $gxpath 的内容,然后打印出 $gxpath->query('b') 等,这样你就可以知道发生了什么。

    作为旁注,您打印 $glist 时除了创建数组之外没有实际做任何事情。

    【讨论】:

    • 其实非对象只能是item(0)的返回。我们知道这是因为投诉是关于缺少property 而不是member function。缺少的属性消息是一个通知,但缺少的功能将是致命的。
    • 谢谢@pguardiario,我的错 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    相关资源
    最近更新 更多