服务器更改后,简单的html dom不再起作用

时间:2019-03-29 13:24:50

标签: php foreach fatal-error simple-html-dom

在将我以前在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 :(得分:0)

只需检查第3行的文件URL。

$ html = file_get_html('URL');

如果使用相对URL,请尝试使用绝对URL。