使用来自rss Feed的php抓取图像

时间:2014-01-26 07:43:23

标签: php rss

PHP

$rss = new DOMDocument();
$rss->load('http://www.autoexpress.co.uk/feeds/all');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
  $item = array (
    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
    'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
    'image' => $node->getElementsByTagName('whattagdoiaddhere')->item(0)->nodeValue,
  );
  array_push($feed, $item);
}
$limit = 3;
for($x=0;$x<$limit;$x++) {
  $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
  $link = $feed[$x]['link'];
  $description = $feed[$x]['desc'];
  $date = date('l F d, Y', strtotime($feed[$x]['date']));
  echo '<div class="news-row-index">';
  echo '<div class="img"><a href="'.$link.'" target="_blank" title="'.$title.'"><img src="'.$image.'" height="79" width="89"></a></div>';
  echo '<div class="details-index"><p><h5><a href="'.$link.'" target="_blank" title="'.$title.'">'.$title.'</a></h5><br />';
  echo '<small><em>Posted on '.$date.'</em></small></p>';
  echo '<p>'.$description.'</p></div>';
  echo '</div>';
}
  1. 我正在尝试从this rss获取图片,但不知道如何获取图片并将其添加到<div class="img"
  2. 我也遇到问题,因为正在收到图片,但它位于$description变量中。
  3. 尝试搜索并找到php rss feed show images但是当我从该答案中添加代码时,我在rss模块中什么都没得到。 请帮帮我,我真的很感激。

1 个答案:

答案 0 :(得分:4)

您可以使用DomDocument loadHTML来解析描述字段并获取图像标记。

foreach ($rss->getElementsByTagName('item') as $node) {
  $htmlStr = $node->getElementsByTagName('description')->item(0)->nodeValue;
  $html = new DOMDocument();        
  $html->loadHTML($htmlStr);
   //get the first image tag from the description HTML
   $img = $html->getElementsByTagName('img')->item(0)->getAttribute('src');
   $item = array (
    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
    'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
    'image' => $img,
  );
  array_push($feed, $item);
}
  print_r($feed);

另外,要检查DOMNodeList是否为空,请尝试

foreach ($rss->getElementsByTagName('item') as $node) {
      $htmlStr = $node->getElementsByTagName('description')->item(0)->nodeValue;
      $html = new DOMDocument();        
      $html->loadHTML($htmlStr);
       //get the first image tag from the description HTML
        $imgTag = $html->getElementsByTagName('img');
        $img = ($imgTag->length==0)?'noimg.png':$imgTag->item(0)->getAttribute('src');
       $item = array (
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        'image' => $img,
      );
      array_push($feed, $item);
    }