0

サイトの RSS フィードを XML として取得して表示しようとしていますが、構文エラーが発生しています。コード全体は次のとおりです。

<?php 
   header('Content-type: text/xml');
   $feeds = file_get_contents('http://rss.news.yahoo.com/rss/topstories');
   $xml = simplexml_load_string($feeds);
   foreach ($xml->channel->item as $item) {
      print $item->title;   
   }
?>

私が得るエラーは

XML 解析エラー: 構文エラー場所: localhost/rssfeed.php

誰かが私が間違っていることを教えてくれますか?

ありがとう

4

1 に答える 1

0

ブラウザが有効なxmlを期待しているヘッダーコンテンツタイプを設定することにより、適切なXMLを出力していないため、XML解析エラーが発生します。

これを試して:

<?php 
header('Content-type: text/xml');

//Load your feed (input)
$feed = file_get_contents('http://rss.news.yahoo.com/rss/topstories');
$feed_xml = simplexml_load_string($feed);

//Create a new SimpleXMLElement object (for output)
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><news/>');

//Loop each item from $feed_xml->channel->item
foreach ($feed_xml->channel->item as $item) {
    //Create a story node for each item
    $node = $xml->addChild('story');
    //Loop each items & add it to the node if its a title or description
    foreach($item as $key=>$value){
        if($key == 'title'){$node->addChild($key, $value);}
        if($key == 'description'){$node->addChild($key, $value);}
    }
}

//Use dom document to format the output nicly, not required
$dom = new DOMDocument('1.0');
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());

echo $dom->saveXML();
/**Output
<?xml version="1.0" encoding="UTF-8"?>
<news>
  <story>
    <title>Ryan says Obama undercuts welfare reform law</title>
    <description>Republican vice presidential candidate Paul Ryan says President Barack Obama wants to ease work requirements for welfare recipients even though that claim has been largely debunked by independent fact checkers.</description>
  </story>
  <story>
    <title>Greenpeace activists storm Russian oil rig</title>
    <description>&lt;p&gt;&lt;a href="http://news.yahoo.com/greenpeace-activists-storm-russian-oil-rig-063813773.html"&gt;&lt;img src="http://l3.yimg.com/bt/api/res/1.2/mSCkTQFkrkzOGPzb3sGLVw--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/2c763e62d83a2717190f6a7067003ab4.jpg" width="130" height="86" alt="Greenpeace activists including Executive Director of Greenpeace International, Kumi Naidoo, board energy giant Gazprom's Arctic oil platform Prirazlomnaya off the North-eastern coast of Russia in the Pechora Sea on Friday, Aug. 24, 2012. Greenpeace activists have stormed a floating oil rig in Russia’s Pechora Sea to protest oil drilling in the Arctic, the environmental organization said on Friday. (AP Photo/ Denis Sinyakov, Greenpeace)" align="left" title="Greenpeace activists including Executive Director of Greenpeace International, Kumi Naidoo, board energy giant Gazprom's Arctic oil platform Prirazlomnaya off the North-eastern coast of Russia in the Pechora Sea on Friday, Aug. 24, 2012. Greenpeace activists have stormed a floating oil rig in Russia’s Pechora Sea to protest oil drilling in the Arctic, the environmental organization said on Friday. (AP Photo/ Denis Sinyakov, Greenpeace)" border="0" /&gt;&lt;/a&gt;Greenpeace activists stormed a floating Russian oil rig early Friday in the open sea to protest oil drilling in the Arctic, the environmental organization said.&lt;/p&gt;&lt;br clear="all"/&gt;</description>
  </story>
  ...
  ...
</news>
 */
?>
于 2012-08-24T14:11:58.140 に答える