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

itemタグを解析する必要があります。

$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 に答える