dpwgzi7987 2019-03-29 13:24
浏览 77

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

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?

  • 写回答

1条回答 默认 最新

  • doujiao2000 2019-03-29 13:40
    关注

    Just check the file URL on line 3.

    $html = file_get_html('URL');

    Try to use absolute URL if you are using a relative one.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口