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

问题描述 投票:0回答:1

在将我之前使用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'));

我猜第二个更好?

php foreach fatal-error simple-html-dom
1个回答
0
投票

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

$ html = file_get_html('URL');

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

© www.soinside.com 2019 - 2024. All rights reserved.