Question

I am trying to receive price data from a google search and this code gives me a "Trying to get property of non-object"

      <?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>";
      ?>

Any help would be nice!

Was it helpful?

Solution

Without any more information, it can be hard to pinpoint where it's coming from.

Odds are it's coming from this line:

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

It could be that the query, item, or nodeValue doesn't exist. Try printing out the contents of $gxpath, then $gxpath->query('b'), etc, so you can figure out what's going on.

As a sidenote, you're printing $glist without actually doing anything with it, other than creating an array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top