2

I have a curl script that ends like this:

  $data = curl_exec($ch);

  curl_close($ch);

  return $data;

}

The $data string it a HTML page with a table on that I want to strip so that I can store the data into a MYSQL database, I have tried using DOM with commands such as:

  // new dom object
  $dom = new DOMDocument();

  //load the html
  $html = str_get_html($returned_content2);
   $dom->strictErrorChecking = false;


  //discard white space 
  $dom->preserveWhiteSpace = false; 

  //the table by its tag name
  $tables = $dom->getElementsByTagName('table'); 

  //get all rows from the table
  $rows = $tables->item(0)->getElementsByTagName('tr'); 

  // loop over the table rows
  foreach ($rows as $row) 
  { 
   // get each column by tag name
      $cols = $row->getElementsByTagName('td'); 
   // echo the values  
      echo $cols->item(0)->nodeValue.'<br />'; 
      echo $cols->item(1)->nodeValue.'<br />'; 
      echo $cols->item(2)->nodeValue;
    } 
}

But keep getting the error:

Fatal error: Call to a member function getElementsByTagName() on a non-object in /home/sdsd/dfdsfsdfds/sdfsdfs/table.php on line 178

Jack Brown
  • 580
  • 3
  • 6
  • 20

2 Answers2

4

You aren't loading the HTML into your DOMDocument at all. Remove this line

$html = str_get_html($returned_content2);

and place this after your preserveWhiteSpace line

$dom->loadHTML($returned_content2);

Before attempting to fetch table rows, you should make sure you've found at least one table, eg

$tables = $dom->getElementsByTagName('table');
if ($tables->length == 0) {
    throw new Exception('No tables found');
}
Phil
  • 157,677
  • 23
  • 242
  • 245
  • Yeah, such a silly mistake! Must be because its so late at night haha, thanks for the help but still not getting the results that I wanted I have added my table html to my first post if you could take a look? Thanks – Jack Brown Dec 18 '12 at 02:52
1

It's rather trivial:

//get all rows from the table
$rows = $tables->item(0)->getElementsByTagName('tr'); 
                 ^^^^^^^

When the document has no tables (e.g. an empty document as you do not load anything into it), that ->item(0) does return NULL. The value NULL does not have that getElementsByTagName method (it's not an object even), hence you see the error message.

Whenever you do something important (or you run into an error), do the needed pre-condition checks. E.g:

$tables = $dom->getElementsByTagName('table');
if (!$tables->length) {
    throw new UnexpectedValueException('Table expected but not found.');
}
hakre
  • 193,403
  • 52
  • 435
  • 836