0

我使用此代码在我的网站中显示新闻,但我总是为它显示的每条新闻得到这个,我不知道为什么会这样,有人可以帮忙吗?

$xml = simplexml_load_file('https://news.google.com.mx/news/feeds?hl=es&gl=mx&q=realmadrid&um=1&ie=UTF-8&output=rss');
print_r($xml);
foreach ($xml as $status) {

$description = $status->item->description;


echo "$description <br> ";

}

在显示每条新闻后我总是得到这个,我知道我正在使用 print_r,但没有 print_r 它只是显示第一条新闻,请帮助:

SimpleXMLElement Object ( [@attributes] => Array ( [version] => 2.0 ) [channel] => SimpleXMLElement Object ( [generator] => NFE/1.0 [title] => realmadrid: Google Noticias [link] => http://news.google.com.mx/news?gl=mx&pz=1&ned=es_mx&hl=es&q=realmadrid [language] => es [webMaster] => news-feedback@google.com [copyright] => ©2012 Google [pubDate] => Sun, 22 Apr 2012 01:00:21 GMT [lastBuildDate] => Sun, 22 Apr 2012 01:00:21 GMT [image] => SimpleXMLElement Object ( [title] => realmadrid: Google Noticias [url] => https://ssl.gstatic.com/news/img/logo/es_mx/news.gif [link] => http://news.google.com.mx/news?gl=mx&pz=1&ned=es_mx&hl=es&q=realmadrid ) [item] => Array ( [0] => SimpleXMLElement Object ( [title] => El Real Madrid apuntilla al Barça en el Camp Nou (1-2) - Republica.com (blog) [link] => http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNGDpztvnga7JP035sqyCqZUrp_Elw&url=http://www.republica.com/2012/04/20/barca-realmadrid-2_482059/ [guid] => tag:news.google.com,2005:cluster=http://www.republica.com/2012/04/20/barca-realmadrid-2_482059/ [pubDate] => Sat, 21 Apr 2012 21:19:44 GMT [description] => 
4

2 回答 2

1

您必须解析项目标签..

$data = file_get_contents('https://news.google.com.mx/news/feeds?hl=es&gl=mx&q=realmadrid&um=1&ie=UTF-8&output=rss');
 $xml_data = simplexml_load_string($data);
 $items = $xml_data->xpath('channel/item');
foreach ($items as $item) {
   echo "title" . $item->title;
}

这是快速编码,所以如果有任何问题,请给我回信;)

于 2012-04-22T03:09:09.090 回答
0

我在检查所有代码后得到如果有人帮助是这样的:

$data = file_get_contents('https://news.google.com.mx/news/feeds?hl=es&gl=mx&q=realmadrid&um=1&ie=UTF-8&output=rss');

$xml = new SimpleXMLElement($data);
$channel = array();
$channel['title']       = $xml->channel->title;

foreach ($xml->channel->item as $item)
{

    //echo $article['title'] = $item->title;
    //echo $article['link'] = $item->link;
    echo $article['pubDate'] = $item->pubDate;
    echo $article['description'] = (string) trim($item->description);

}
于 2012-04-22T23:22:35.603 回答