Search code examples
phpparsinghtml-parsingsimple-html-dom

Error using PHP Simple HTML DOM parser


    if (!is_null($elements)) {          

        $embeds = array();
        foreach ($elements as $element) {

            if (trim(strip_tags($element->innertext)) == $episode_term) {
                $html2 = file_get_html($element->href);
                $elements2 = $html2->find('#streamlinks .sideleft a');
                if (!is_null($elements2)) {
                    foreach ($elements2 as $element) {
                        $html3 = file_get_html($element->href);
                        $iframe_element = $html3->find('.frame', 0);
                        if (!is_null($iframe_element)) {
                            $embed = $misc->buildEmbed($iframe_element->src);
                            if ($embed) {
                                $embeds[] = array(
                                    "embed" => $embed,
                                    "link" => $iframe_element->src,
                                    "language" => "ENG",
                                );
                            }
                        }
                    }                        
                }
            }
        }
        return $embeds;
    }

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

                $elements2 = $html2->find('#streamlinks .sideleft a');

so its confusing as to what is causing this error to appear in my error log file?


Solution

  • I'd try to output $element->href befor you do the file_get_html. If the file_get_html can't get a page $html2 stays uniinitialized and you can't use find on it.

    Beside that you could build a check wether $html2 is set after the file_get_html and output an error if not. I usually use something like this:

    if($html2 == false || $html2 == NULL){
      // no html found
    }else{
      // html found
    }