Reputation: 2742
I keep getting this error from my code and I have no idea what I am doing wrong, this happens on occasions and it seems to work when it wants to
error
Call to a member function find() on a non-object in C:\xampp\htdocs\sites\P\Find.php on line 265
I've basically created a crawler which searches a webpage for an element on the webpage, sometimes this element may not be present on the page, and I check for this by using the if statement.
line 265 refers to
if($page->find('div#olpDivId span.price'))
code
$page = file_get_html('http://www.amazon.co.uk/dp/0304362212');
if($page->find('div#olpDivId span.price')){
foreach($page->find('div#olpDivId span.price') as $p){
$i[] = floatval($p->plaintext);
}
}
if the book does not exist the crawler goes to a blank "sorry product does not exist" page Am I doing something wrong? any help would be appreciated
Upvotes: 0
Views: 1699
Reputation: 29482
file_get_html
can return false (if it was unable to fetch content from webpage), so you should check for it before using any method on $page
$page = file_get_html('http://www.amazon.co.uk/dp/0304362212');
if($page !== FALSE){
foreach($page->find('div#olpDivId span.price') as $p){
$i[] = floatval($p->plaintext);
}
}
Upvotes: 1