simple_html_dom の仕組みを理解し、特定のカテゴリからデータを取得しようとしています。そこで、いくつかのテストを行うために wp サイトをセットアップしました。
公式ウェブサイトからコードを取得し、それに合わせていくつかの変更を加えました。
// Create DOM from URL
$html = file_get_html('http://localhost/mydemosite/category/sports/');
// Find all article blocks
foreach($html->find('div.article') as $article) {
$item['title'] = $article->find('div.post-title', 0)->plaintext;
$item['thumb'] = $article->find('div.post-thumbnail', 0)->plaintext;
$item['details'] = $article->find('div.entry', 0)->plaintext;
$articles[] = $item;
}
print_r($articles);
これを実行しているときにエラーが発生しました:
Notice: Undefined variable: articles in C:\xampp\htdocs\mydemosite\test.php on line 28
28行目はprint_r($articles);
私の構造は次のとおりです。
<article class="item-list item_1">
<h2 class="post-title"><a href="http://localhost/mydemosite/category/sports/demo-post" title="mydemo post" rel="bookmark">my demo post 1</a></h2>
<p class="post-meta">
<span class="tie-date">2 mins ago</span>
<span class="post-comments">
<a href="http://localhost/mydemosite/category/sports/demo-post/#disqus_thread" title="my demo post 1" data-disqus-identifier="1 http://localhost/mydemosite/category/sports/?p=1"></a></span>
</p>
<div class="post-thumbnail">
<a href="http://localhost/mydemosite/category/sports/demo-post/" title="my demo post 1" rel="bookmark">
<img width="300" height="160" src="http://localhost/mydemosite/wp-content/uploads/demo-post-300x160.jpg" class="attachment-tie-large wp-post-image" alt="my demo post 1">
</a>
</div>
<!-- post-thumbnail /-->
<div class="entry">
<p>Hello world... this is a demo post description, so if you want to read more...</p>
<a class="more-link" href="http://localhost/mydemosite/category/sports/demo-post">Read More »</a>
</div>
<div class="clear"></div>
</article>