2014-01-23 56 views
0

我試圖加載和我需要一個WordPress飼料重建,但我得到的錯誤:在第2行 錯誤在第3欄:xmlParsePI:沒有目標名稱加載WordPress的飼料

的代碼加載和重建WP飼料波紋管:

<?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>'; 
?> 

和進料看起來像這樣:

<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

那真的是飼料是什麼樣子?因爲它不是格式良好的XML。它需要一個XML聲明作爲第一行,例如'<?xml version =「1.0」encoding =「UTF-8」?>'。我猜這可能會導致你的錯誤,因爲'xmlParsePI'用於解析處理指令,也可能用於嘗試解析XML聲明(即使它在技術上不是PI)。 –

回答

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; 
} 
?>