dharanbro
dharanbro

Reputation: 1342

Trying to get property of non object Simple HTML DOM Parser

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?

Upvotes: 3

Views: 11203

Answers (3)

theTypan
theTypan

Reputation: 5887

To extend @Barmar answere, you could also do as :

$wholeContent=$productPage->find('div[id=searchResultsDiv]', 0);
$wholeContent->plaintext;

Upvotes: 2

Barmar
Barmar

Reputation: 781503

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;
}

Upvotes: 13

Sushant
Sushant

Reputation: 1414

Make sure there are no duplicate #searchResultsDiv in your code.

In that case find() would return the array of object rather than object itself

Upvotes: 0

Related Questions