致命錯誤:未捕獲錯誤:在 C 中的 bool 上呼叫成員函數 find() 堆疊追蹤:#0 {main} 在第 16 行在 C 中拋出
P粉878510551
P粉878510551 2023-12-28 23:57:42
0
1
368

我在上面收到此錯誤,我的程式碼如下。我的程式碼有什麼問題嗎?任何幫助都是感激的。我不確定它是否與我用來運行它的軟體 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學習者快速成長!