php - 如何解析博客rss文件

时间:2013-08-23 21:07:00

标签: php xml encoding rss

我正在以RSS格式下载一些博客。这是一个例子:

<rss xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"     xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title>
john doe clippingsconverter - Google Blog Search
</title>
<link>
http://www.google.com/search?hl=en&q=john+doe+clippingsconverter&tbm=blg
</link>
<description>1 result</description>
<opensearch:totalResults>1</opensearch:totalResults>
<opensearch:startIndex>1</opensearch:startIndex>
<opensearch:itemsPerPage>1</opensearch:itemsPerPage>
<item>
<title>5 Best Kindle Tips and Tricks - Make Tech Easier</title>
<link>
http://www.maketecheasier.com/5-best-kindle-tips-and-tricks/2010/10/16
</link>
<description>
<em>John Doe</em>. You can convert your clippings file to word, excel and pdf at  http://www.<em>clippingsconverter</em>.com &middot; 28th April 2011 02:53:00 Reply. Anonymous. yes, http://www.<em>clippingsconverter</em>…. is a true gem. I also use this&nbsp;<b>...</b>
</description>
<dc:publisher>Make Tech Easier</dc:publisher>
<dc:creator>Abhiroop Basu</dc:creator>
<dc:date>Sat, 16 Oct 2010 12:00:53 GMT</dc:date>
</item>
</channel>
</rss>

我正在使用此代码:

$feed = file_get_contents($SearchURL);
$xml = new SimpleXmlElement($feed);

foreach ($xml->channel->item as $entry){
  echo $entry->title;
  echo $entry->description;
}

我收到此错误消息:

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 21: parser error : Input is not proper UTF-8, indicate encoding ! 

我注意到下载的Feed 将此行放在顶部,这可能会解释错误。

<?xml version="1.0" encoding="UTF-8"?>

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

使用utf8_encode检查

$feed = utf8_encode(file_get_contents($SearchURL));