Reputation: 251
I just learning about simple_html_dom.php, I try to get the h1 content with some class.
<h1 class="entry-title">example for the header</h1>
here the raw html file from the website that i want to get the content.
<header class="entry-header">
<div class="entry-meta">
<span class="cat-links"><a href="https://xxxxx/2016/08/11/xxxxxxx" rel="category tag">News</a></span>
</div>
<h1 class="entry-title">example for the header</h1>
<div class="entry-meta">
<span class="entry-date"><a href="https://xxxxx/2016/08/11/xxxxxxx" rel="bookmark"><time class="entry-date" datetime="2016-08-11T11:54:07+00:00">11 August 2016</time></a></span>
<span class="byline"><span class="author vcard"><a class="url fn n" href="https://xxxxx/2016/08/11/xxxxxxx" rel="author">wndwnrt</a></span></span>
<span class="comments-link"><a href="https://xxxxx/2016/08/11/xxxxxxx">1 Comment</a></span>
</div>
</header>
here my code to get the h1 class="entry-title" content (example for the header)
<?php
require_once __DIR__.'/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('https://xxxxx/2016/08/11/xxxxxxx');
$header_1 = $html->find('h1[class="entry-title"]')->innertext;
?>
<table border="1">
<thead>
<tr>
<th><?php echo $header_1; ?></th>
</tr>
</thead>
</table>
when i run the code, the result is error:
Trying to get property of non-object
can anyone tell where is the error? and what i should to do? Thank you very much.
Upvotes: 0
Views: 2323
Reputation: 502
Yes you see that error because you are passing only one argument to the find function.
$header_1 = $html->find('h1[class="entry-title"]')->innertext
now try this:
$header_1 = $html->find('h1[class="entry-title"]',0)->innertext
because you also have to pass the number of the h1 you are trying to get!
Upvotes: 5