Trying to get property of non object Simple HTML DOM Parser

dharanbro picture dharanbro · Sep 9, 2014 · Viewed 8.6k times · Source

I am trying to get content of <div class="clearfix" id="searchResultsDiv"> from the link in my code. Whatever I try it shows a Notice as follow

Notice: Trying to get property of non-object in E:\xampp\htdocs\homeshop\index.php on line 20

$link="http://www.homeshop18.com/samsung/categoryid:3027/search:samsung/inStock:true/sort:Popularity/?it_category=MN&it_action=MA-MMAA01&it_label=MN-MMAA01-140906000003-PD-MA-OT-OT-SR_Samsung-0_0-0-MNU101-MA-140730-OT-OT-SR&it_value=0";
$productPage=file_get_html($link);
$wholeContent=$productPage->find('div[id=searchResultsDiv]');
echo $wholeContent->plaintext; //line 20

There is an element with this id but still i cant do it. Where I am wrong?

Answer

Barmar picture Barmar · Sep 9, 2014

find returns an array of matching elements, even if there's just one match. You need to index it to get the element.

if ($wholeContent) {
    echo $wholeContent[0]->plaintext;
}