Search code examples
phpsimple-html-dom

Call to a member function find() on a non-object simpleHTMLDOM


I am trying to read a link from one page, print the URL, go to that page, and read the link on the next page in the same location, print the url, go to that page (and so on...).

All I'm doing is reading the URL and passing it as an argument to the get_links() function until there are no more links.

This is my code but it throws:

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

Anyone know how to fix this?

  <?php
$mainPage = 'https://www.bu.edu/link/bin/uiscgi_studentlink.pl/1346752597?ModuleName=univschr.pl&SearchOptionDesc=Class+Subject&SearchOptionCd=C&KeySem=20133&ViewSem=Fall+2012&Subject=&MtgDay=&MtgTime=';

get_links($mainPage);

function get_links($url) {
    $data = new simple_html_dom();
    $data = file_get_html($url);

    $nodes = $data->find("input[type=hidden]");
    $fURL = $data->find("/html/body/form");
    $firstPart = $fURL[0]->action . '<br>';

    foreach ($nodes as $node) {
        $val = $node->value;
        $name = $node->name;
        $name . '<br />';
        $val . "<br />";

        $str1 = $str1 . "&" . $name . "=" . $val;
    }
    $fixStr1 = str_replace('&College', '?College', $str1);
    $fixStr2 = str_replace('Fall 2012', 'Fall+2012', $fixStr1);
    $fixStr3 = str_replace('Class Subject', 'Class+Subject', $fixStr2);

    $fixStr4 = $firstPart . $fixStr3;
    echo $nextPageURL = chop($fixStr4);
    get_links($nextPageURL);
}
?>

Solution

  • Alright so I was using the load->file() function somewhere in my code and did not see it until I really scraped through it. Finally have a running script :) The key is to use file_get_html instead of loading the webpage as an object using the load->file() function.