0

I get this error above, My code is as below. Anything wrong with my code? any help is appreciate. I'm not sure if it has something to do with the software im using to run it which is XAMPP Apache. I am currently trying to retrieve information from a web page with a product codes in an excel. I managed to run about 900+ product codes, but suddenly i am receiving this error above.

<?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';

?>

1 Answers1

0

This happens because file_get_html() is returning a boolean (probably false due to some error like invalid url).

Your code is not checking against failures.

I suggest you adding something like:

if (!$page) {
  // Do some error handling, logging
  continue; // skip to next iteration on your loop.
}
Elias Soares
  • 9,884
  • 4
  • 29
  • 59
  • thanks! I seem to have another error, could you help me? 'Warning: file_get_contents(): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C on line 82' My codes are still the same except I included your answer. –  Jun 13 '22 at 06:50
  • Your URL is returning a bad request error. That's not your code's problem. – Elias Soares Jun 13 '22 at 06:55
  • is there anything i can do to fix it? –  Jun 13 '22 at 06:59
  • It depends on what is the url you are trying do access. Bad request happens when you are trying to access some invalid url or missing some essential data. Try accessing the failed url on browser to see if some error is being presented – Elias Soares Jun 13 '22 at 07:23