После смены сервера простой html dom больше не работает - PullRequest
0 голосов
/ 29 марта 2019

После переноса моего ранее работающего кода на 5.6 PHP на новый хост и сервер с PHP 7.2, я теперь получаю это Fatal error: Uncaught Error: Call to a member function find() on array ....Как это исправить?

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

}
?>

ОБНОВЛЕНИЕ Итак, похоже, что источником ошибки является код file_get_html, который не работает идеально с PHP 7.

Я нашелдва обхода:

1) Скручивание

// 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) Еще через str_get_html

$html = str_get_html(file_get_contents('RELATIVE PATH'));

Полагаю, второе лучше?

1 Ответ

0 голосов
/ 29 марта 2019

Просто проверьте URL файла в строке 3.

$ html = file_get_html ('URL');

Попробуйте использовать абсолютный URL, если вы используете относительный.

...