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
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');
}
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.');
}