正在加载wordpress feed

时间:2014-01-23 20:19:45

标签: php xml wordpress

我正在尝试加载和重建我需要的wordpress feed但我收到错误: 第3行第2行的错误:xmlParsePI:无目标名称

加载和重建wp feed的代码如下:

<?php header('Content-Type: text/xml'); ?>
<?php
echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"; 
?>

<?
// specify url of xml file
$url = "http://www.mysite.com/category/celeb-news/feed/";
$xml = simplexml_load_file($url);


echo '<channel>';
foreach($xml->item as $item)
{

echo '
<item>
<ID><![CDATA[1]]></ID>
<title>'.$item->title.'</title>
<link>'.$item->link.'</link>
<category><![CDATA['.$item->category.']]></category>
</item>';

}
echo '</channel>';
?>

Feed看起来像这样:

<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
<channel>
<title>Exciting news</title>
<atom:link href="http://www.mysite.com/category/celeb-news/feed/" rel="self" type="application/rss+xml"/>
<link>http://www.mysite.com</link>
<description>Celebrities World</description>
<lastBuildDate>Thu, 23 Jan 2014 17:51:58 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>http://wordpress.org/?v=3.8</generator>
<item>
<title>
Justin  arrested in Miami for DUI and going DOUBLE the speed limit!
</title>
<link>
http://www.mysite.com/justin-miami-beach-dui-double-speed-limit/
</link>
<comments>
http://www.mysite.com/justin-miami-beach-dui-double-speed-limit/#comments
</comments>
<pubDate>Thu, 23 Jan 2014 17:22:55 +0000</pubDate>
<dc:creator>
<![CDATA[ adminadmin ]]>
</dc:creator>
<category>
<![CDATA[ Celeb News ]]>
</category>
<guid isPermaLink="false">http://www.mysite.com/?p=380</guid>
<description>
<![CDATA[
Justin He was charged with resisting arrest without violence. Justin Bieber was driving in a residential neighborhood and was going up to [&#8230;]
]]>
</description>
<content:encoded>
<![CDATA[
<p><img class="alignnone size-full wp-image-382" alt="Justin arrested" src="http://www.mysite.com/wp-content/uploads/2014/01/Justin.jpg" width="1920" height="1200" /></p> <p><a title="justin bieber" href="http://en.wikipedia.org/" target="_blank">Justin
]]>
<![CDATA[
Balh Blah Blah
]]>
</content:encoded>
<wfw:commentRss>...</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>

知道我错了什么?

1 个答案:

答案 0 :(得分:0)

我使用了这个解决方案,效果很好:

<?
$rss = new DOMDocument();
$rss->load('http://www.mysite.com/category/celeb-news/feed/');
$feed = array();

foreach ($rss->getElementsByTagName('item') as $node) {

$title = $node->getElementsByTagName('title')->item(0)->nodeValue;
$desc = $node->getElementsByTagName('description')->item(0)->nodeValue;
$link = $node->getElementsByTagName('link')->item(0)->nodeValue;
$date = $node->getElementsByTagName('pubDate')->item(0)->nodeValue;
}
?>