注意:试图获取非对象的属性错误


PHP - Simple HTML DOM: Notice: Trying to get property of non-object error

我一直试图修复这个错误最长的时间现在,我只是不能修复它。

我正在尝试获取文章图像,url和url标题。由于某种原因,我一直得到上述错误的代码:

<?php
$html = file_get_html("http://articlesite.com/");
if($html){
foreach ($html->find('.index_item a img') as $div) {
$articlePoster = $div->src;
$grabURL = $html->find('.index_item a');
/*Error Here -->*/$articleURL = $grabURL->href;
/*And Here -->*/$rawTitle = $grabURL->title;
echo '<div class="articleFrame"><a href="'.$articleURL.'"><img src="'.$articlePoster.'" width="125" height="186"/><br><p class="title">'.$rawTitle.'</p></a></div>';
}
}else{
echo '<h1>'."Sorry.".'</h1>';
}
?>

任何想法?谢谢。

$html->find('xxxxx')返回一个数组,所以您需要遍历它——即

foreach ($html->find('.index_item a img') as $div) {
    $articlePoster = $div->src;
    foreach ($html->find('.index_item a') as $grabURL) {
        $articleURL = $grabURL->href;
        $rawTitle = $grabURL->title;
    (etc.)