Search code examples
phphtmlsimple-html-dom

Creating default object from empty value - Simple HTML DOM


ok so the goal of this file to read the titles from some xml feed, and modify a file I have locally. I have gotten the simplexml to work, but everything went downhill when I added the simple html dom. When I run the file, I keep getting:

Creating default object from empty value

I have looked at other solutions on stack overflow that seemed to work for others, such using:

$title = new stdClass;

So I'm not so sure what the problem is. Thanks for your help in advance.

require("simple_html_dom.php");

$html = file_get_html('../index.html');
$xml = simplexml_load_file("https://www.figurescreed.moe/feed/") or 
die("Error: Cannot create object");;

for ($x = 0; $x < 3; $x++) {
    $title = new stdClass;
    $element = new stdClass;
    $title = $xml->channel->item[$x]->title;
    $element = $html->find('div[id=title]');
    $element[$x]->innertext = $title;
    echo $html;

}

?>

Solution

  • Try to check that $element is an array, with at least 3 elements after this line:

    $element = $html->find('div[id=title]');
    

    You could check it with

    echo '<pre>' . htmlspecialchars (print_r ($element, true)) . '</pre>';
    

    or if you are running it from the command line:

    echo print_r ($element, true);
    

    If there aren't 3 elements in it, you are probably getting the error on this line:

    $element[$x]->innertext = $title;
    

    Because $element[$x] refers to a non-existent array element.

    Probably there aren't 3 items in your html file that match div[id=title]

    These 2 lines are unnecessary, because you are overwriting them immediately afterwards:

    $title = new stdClass;
    $element = new stdClass;