致命错误:未捕获错误:在 C 中的 bool 上调用成员函数 find() 堆栈跟踪:#0 {main} 在第 16 行在 C 中抛出
P粉878510551
P粉878510551 2023-12-28 23:57:42
0
1
370

我在上面收到此错误,我的代码如下。我的代码有什么问题吗?任何帮助都是感激的。我不确定它是否与我用来运行它的软件 XAMPP Apache 有关。我目前正在尝试从网页中检索包含 Excel 中产品代码的信息。我设法运行了大约 900 多个产品代码,但突然收到上面的错误。

<?php
// example of how to use basic selector to retrieve HTML contents
include('simple_html_dom.php');

$file = fopen("Book1.csv","r");
$file2 = fopen("test.csv","w");
$links = [];

while (($result = fgetcsv($file)) !== false)
{
    $link = "https://mall/Product/".$result[0];
    $links[] = $link;

    $row_data = [];
    $page = file_get_html($link);
    $product_details = $page->find('.ProductDetailsTable tr'); //line 16
    if(count($product_details)==0) {
        $row_data[] = $result[0];
        $row_data[] = 'not found'; 
        fputcsv($file2, $row_data);
        continue;
    }

    //second method
    $article_number = '';
    $product_description = '';
    $product_family = '';
    $product_lifecycle = '';
    $plm_date = '';
    $notes = '';
    $EAN = '';
    $UPC = '';
    $country_of_origin = '';

    foreach($product_details as $table_row) {    
        if(count($table_row->find('td'))==1){
            //ignore
        } elseif(count($table_row->find('td'))==2) {
            $key = $table_row->find('td')[0]->plaintext;
            $value = $table_row->find('td')[1]->plaintext;
            if($key=="EAN") {
                $EAN = $value;
            }
            elseif($key=='Article Number (Market Facing Number)') {
                $article_number = $value;
            } elseif ($key=='Product Description') {
                $product_description = $value;
            } elseif ($key=='Product family') {
                $product_family = $value;
            }elseif ($key=='Product Lifecycle (PLM)') {
                $product_lifecycle = $value;
            }elseif ($key=='PLM Effective Date') {
                $plm_date = $value;
            }elseif ($key=='Notes') {
                $notes = $value;
            }elseif ($key=='UPC') {
                $UPC = $value;
            }elseif ($key=='Country of origin') {
                $country_of_origin = $value;
            }    
        } 
    }
    $row_data[] = trim($article_number);
    $row_data[] = trim($product_description);
    $row_data[] = trim($product_family);
    $row_data[] = trim($product_lifecycle);
    $row_data[] = trim($plm_date);
    $row_data[] = trim($notes);
    $row_data[] = trim($EAN);
    $row_data[] = trim($UPC);
    $row_data[] = trim($country_of_origin);
    fputcsv($file2, $row_data);
}

fclose($file);
fclose($file2);
echo 'done';

?>

P粉878510551
P粉878510551

全部回复(1)
P粉237647645

发生这种情况是因为 file_get_html() 返回一个布尔值(可能是 false 由于一些错误,例如无效的 url)。

您的代码没有检查失败。

我建议您添加以下内容:

if (!$page) {
  // Do some error handling, logging
  continue; // skip to next iteration on your loop.
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!