Search code examples
phphtmlhtml-tablesimple-html-dom

Get table value using PHP Simple HTML DOM


I'm writing this PHP to read the data from the following website, and the write it into database.

Here's the code:

<?php
require('simple_html_dom.php');
$html = file_get_html('http://backpack.tf/pricelist/spreadsheet');
$data = $html->find('.table tr td[1]');  
foreach($data as $result)
{
echo $result->plaintext . '<br />';
}
?>

I intended to get all the data in the tds and even the attribute inside the trs.
So, I tried by getting them in plain text first. By far the code returns:

Fatal error: Call to a member function find() on a non-object

How can I solve and improve the code?


Solution

  • The following code is working for your example. It could be the memory limit for your executing script that's causing trouble.

    ini_set('memory_limit','160M');
    require('simple_html_dom.php');
    $url = 'http://backpack.tf/pricelist/spreadsheet';
    
    $html = new simple_html_dom();
    $html->load_file($url);
    
    $data = $html->find('.table tr td[1]');  
    foreach($data as $result)
    {
        echo $result->plaintext . '<br />';
    }