After transferring my previously working code on 5.6 PHP to a new host and server with PHP 7.2, I'm now getting this Fatal error: Uncaught Error: Call to a member function find() on array ...
. How do I fix this?
<?php
// Get Source
$html = file_get_html('URL');
// Get needed table
$table = $html->find('table',1);
// Find each row, starting with the 2nd, and echo the Cells
foreach($table->find('tr') as $rowNumber => $row) {
if ( $rowNumber < 1 ) continue;
$cell = $row->find('td', 0)->plaintext;
echo $cell;
$cell2 = $row->find('td', 1)->plaintext;
echo $cell2;
}
?>
UPDATE
So it seems the source of the error is the file_get_html
code which doesn't work perfectly with PHP 7.
I've found two go-arounds:
1) Through curl
// Curl-Verbindung zu HTM-Datei
$base = 'FULL PATH';
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $base);
curl_setopt($curl, CURLOPT_REFERER, $base);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$str = curl_exec($curl);
curl_close($curl);
// Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($str);
2) Another through str_get_html
$html = str_get_html(file_get_contents('RELATIVE PATH'));
I guess the second is better?