Bing新闻提供给PHP

时间:2015-09-06 12:18:22

标签: php rss bing

这是我的代码:

$rss = new DOMDocument();
$rss->load('http://www.bing.com/news/search?q=obama&format=rss');
$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,
        'image' => $node->getElementsByTagName('News:Image')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        );
    array_push($feed, $item);
}
$limit = 1;
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 '<item>';
    echo '<title>'.$title.'</title>';
    echo '<link>'.$link.'</link>';
    echo '<description>'.$description.'</description>';
    echo '<News:Image>'.$image.'</News:Image>';
    echo '<pubDate>'.$date.'</pubDate>';
    echo '</item>';
    }

我无法在标签 新闻:图片 中提取图片网址,其余的都可以

Feed输出位于此link

1 个答案:

答案 0 :(得分:0)

在输出部分,您没有定义变量$image,而您需要搜索的标记是@u_mulder所说的Image。以下内容包含了这些更改,并且似乎输出了所需的结果: -

$rss = new DOMDocument();
$rss->load('http://www.bing.com/news/search?q=obama&format=rss');
$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,
        'image' => $node->getElementsByTagName('Image')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
    );
    array_push($feed, $item);
}

$limit = count( $feed );
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']));
    $image = $feed[$x]['image'];

    echo '<item>';
    echo '<title>'.$title.'</title>';
    echo '<link>'.$link.'</link>';
    echo '<description>'.$description.'</description>';
    echo '<Image>'.$image.'</Image>';
    echo '<pubDate>'.$date.'</pubDate>';
    echo '</item>';
}