如何在WordPress中读取XML文件?

时间:2012-05-04 作者:user478

我有一个feed链接http://www.wdcdn.net/rss/presentation/library/client/iowa/id/128b053b916ea1f7f20233e8a26bc45d 我必须阅读feed的所有元素,包括学分,下面是feed的显示方式

<item>
  <title>
    <![CDATA[Dave&#39;s Lawnmower Bish Bash]]>
  </title>
  <link>http://cache.wdcdn.net/cdn/asset/view/client/filmconstruction/package/library/id/918787/format/o/h/503e4d2418da8c167f7a75d25638a35e/?7a6013aef28682d61703dff120d21b12266b54a2a637283d7d4f0a0843a6f755b7d8fa6f17d0ac1585d9530a052e29240a1a3c75fb</link>
  <guid>http://filmconstruction.wdcdn.net/l/p/?presentation=a211defdbe060c7e8f42e884b7e8f584#918787</guid>
  <description>
    <![CDATA[DW_MLA_Lamb_DavesLawnMowerBishBash_30.mov]]>
  </description>
  <pubDate>Mon, 30 Apr 2012 21:49:55 -0700</pubDate>
  <enclosure url="http://cache.wdcdn.net/cdn/asset/view/client/filmconstruction/package/library/id/918787/format/o/h/503e4d2418da8c167f7a75d25638a35e/DW_MLA_Lamb_DavesLawnMowerBishBash_30.mov?7a6013aef28682d61703dff120d21b12266b54a2a637283d7d4f0a0843a6f755b7d8fa6f17d0ac1585d9530a052e29240a1a3c75fb.mov" type="video/quicktime" length="6289971"/>
  <media:content url="http://cache.wdcdn.net/cdn/asset/view/client/filmconstruction/package/library/id/918787/format/o/h/503e4d2418da8c167f7a75d25638a35e/DW_MLA_Lamb_DavesLawnMowerBishBash_30.mov?7a6013aef28682d61703dff120d21b12266b54a2a637283d7d4f0a0843a6f755b7d8fa6f17d0ac1585d9530a052e29240a1a3c75fb.mov" fileSize="6289971" type="video/quicktime" duration="0" height="360" width="640"/>
  <media:thumbnail url="http://cache.wdcdn.net/cdn/asset/view/client/filmconstruction/package/library/id/918787/format/t/size/lg/h/87884cce4f657e643d3df42ccc623101/library_918787_lg.jpg?7a6013aef28682d61703dff120d21b12266b54a2a637283d7d4f0a0843a6f755b7d8fa6f17d0ac1585d9530a052e29240a1a3c75fb.jpg" height="360" width="640"/>
  <media:thumbnail url="http://cache.wdcdn.net/cdn/asset/view/client/filmconstruction/package/library/id/918787/format/t/size/sm/h/87884cce4f657e643d3df42ccc623101/library_918787_sm.jpg?7a6013aef28682d61703dff120d21b12266b54a2a637283d7d4f0a0843a6f755b7d8fa6f17d0ac1585d9530a052e29240a1a3c75fb.jpg" height="101" width="180"/>
  <media:credit role="client" scheme="urn:feeds-wiredrive-com:credits">
    <![CDATA[MLA]]>
  </media:credit>
  <media:credit role="agency" scheme="urn:feeds-wiredrive-com:credits">
    <![CDATA[BMF]]>
  </media:credit>
  <media:credit role="director" scheme="urn:feeds-wiredrive-com:credits">
    <![CDATA[David Wood]]>
  </media:credit>
  <media:keywords>
    <![CDATA[Humour]]>
  </media:keywords>
</item>
我用这个代码来阅读内容,但不能用这个代码获得所有学分

$file   = file_get_contents(\'http://www.wdcdn.net/rss/presentation/library/client/iowa/id/128b053b916ea1f7f20233e8a26bc45d\'); 
$xml    =  simplexml_load_string($file);

$doc = new DOMDocument();
$doc->load(\'http://www.wdcdn.net/rss/presentation/library/client/iowa/id/128b053b916ea1f7f20233e8a26bc45d\');
$arrFeeds = array();
foreach ($doc->getElementsByTagName(\'item\') as $node) {
        $itemRSS = array ( 

                \'title\' => $node->getElementsByTagName(\'title\')->item(0)->nodeValue,                

                \'guid\' => $node->getElementsByTagName(\'guid\')->item(0)->nodeValue,
    \'content\' => $node->getElementsByTagNameNS(\'http://search.yahoo.com/mrss/\', \'content\')->item(0)->getAttribute(\'url\'),
    \'vwidth\' => $node->getElementsByTagNameNS(\'http://search.yahoo.com/mrss/\', \'content\')->item(0)->getAttribute(\'width\'),
    \'vheight\' => $node->getElementsByTagNameNS(\'http://search.yahoo.com/mrss/\', \'content\')->item(0)->getAttribute(\'height\'),
    \'thumbnail\' => $node->getElementsByTagNameNS(\'http://search.yahoo.com/mrss/\', \'thumbnail\')->item(0)->getAttribute(\'url\'),
    \'credit\' => $node->getElementsByTagNameNS(\'http://search.yahoo.com/mrss/\', \'credit\')->item(0)->getAttribute(\'role\') 
                );
        array_push($arrFeeds, $itemRSS);
}
如何读取每个项目的所有学分?有人能帮我找到解决办法吗?提前感谢

2 个回复
最合适的回答,由SO网友:Rarst 整理而成

WordPress捆绑和包装SimplePie类,用于feed下载、缓存和处理。使用它检索命名空间数据的方式如下:

$feed  = fetch_feed( \'http://www.wdcdn.net/rss/presentation/library/client/iowa/id/128b053b916ea1f7f20233e8a26bc45d\' );
$items = $feed->get_items();

foreach ( $items as $item ) {

    $credit = $item->get_item_tags( \'http://search.yahoo.com/mrss/\', \'credit\' );
    var_dump( $credit );
}
请参见fetch_feed() 在法典中。

SO网友:fischi

无论如何,如果你这样做:

$medias = $node->getElementsByTagName(\'media\');
foreach ( $medias as $media ) {
    $media_items[] = $media->nodeValue;
}
$itemRSS = array (
    \'medias\' => $media_items,
    // all the stuff you did
);
你应该得到你的结果。

结束

相关推荐

如何保护WordPress XMLRPC的安全?

XMLRPC非常适合远程发布到WordPress,但也存在许多安全问题。如何使其更安全?更具体地说,只有来自intranet的用户才能通过XMLRPC进行发布。WP目前正在Lighttpd和php5上运行。3.