2

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);
}
?>
j0k
  • 22,600
  • 28
  • 79
  • 90
wandersolo
  • 69
  • 1
  • 3
  • 11
  • Additionally, I do get the $nextPageURL to echo correctly so I know the code works but only for the first page. – wandersolo Sep 09 '12 at 03:40
  • This sounds vaguely familiar to [this question...](http://stackoverflow.com/questions/12335394/call-to-a-member-function-find-on-a-non-object-when-inserting-data-using-php-m/12335425#12335425) – David Sep 09 '12 at 03:49
  • It throws the same error but I am using the file_get_html() method in this case. Also I think its an issue with recursion but I'm not sure? – wandersolo Sep 09 '12 at 03:53
  • [This](http://stackoverflow.com/a/6190708/1415625) might be your problem - simple, but it might be the case. – David Sep 09 '12 at 04:03
  • I have the imported the simple_html_dom.php library just didn't copy it to the code here. Also that's definitely not the case since my function works if only just once. – wandersolo Sep 09 '12 at 04:16
  • Apparently this [is a bug](http://stackoverflow.com/a/6832539/1415625). Consider using something like [PHPQuery](http://code.google.com/p/phpquery/) which, in my experience, is much better – David Sep 09 '12 at 04:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16472/discussion-between-wandersolo-and-david) – wandersolo Sep 09 '12 at 19:37

1 Answers1

0

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.

wandersolo
  • 69
  • 1
  • 3
  • 11