现在我正在研究用于PHP的RSS阅读器。我几乎完成但我遇到了一些问题。问题偶尔会意外开始。有时相反,我有文本“字符串无法解析为XML”。我认为这是因为某些RSS频道在PHP中显示有问题。当我发现并最终删除有问题的渠道问题消失但当我再次添加它问题也没有apper。所以看起来它是某种时间错误。我不知道。
你有什么建议吗?
显示RSS项目
<div class="txt-content">
<?php include "feeds.php";
$urls_pl = array('http://feeds.feedburner.com/sportowefakty/TtDh','http://www.goal.pl/rss.php','http://feeds.feedburner.com/igol?format=xml','http://feeds.feedburner.com/mufcpl','http://fcbayern.pl/news/rss','http://atleticopoland.com/news/rss','http://feeds.feedburner.com/WiadomociPrzegladsportowypl','http://www.futbolnews.pl/informacje/aktualnosci/rss.xml','http://sport.wp.pl/kat,1726,rss.xml?ticaid=1122f8','http://interia.pl.feedsportal.com/c/34004/f/625102/index.rss','http://kanonierzy.com/rss.shtml','http://www.realmadrid.pl/aktualnosci.xml','http://www.chelsealive.pl/news/rss','http://feeds.feedburner.com/devilpage/oNUh','http://www.fcbarca.com/feed');
$urls = array('http://football-italia.net/rss.xml','http://www.fifa.com/rss/index.xml','http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=uk','http://www.lequipe.fr/rss/actu_rss_Football.xml','http://rss.kicker.de/news/fussball','http://www.dailymail.co.uk/sport/index.rss','http://www.skysports.com/rss/0,20514,11095,00.xml','http://www.goal.com/en/feeds/news?fmt=rss&ICID=HP','http://uefa.com/rssfeed/uefachampionsleague/rss.xml','http://ir.manutd.com/corporate.rss?c=133303&Rule=Cat=news~subcat=ALL','http://feeds.feedburner.com/daily-star-football','http://www.football.co.uk/divisions/european/rss.xml','http://feeds.feedburner.com/Men-Sport','http://sportbild.bild.de/services/rss/sportbild-bundesliga-10186136,sort=1,n=25,view=rss2.sport.xml','http://www.allgemeine-zeitung.de/sport/national-und-international/fussball/index.rss');
try
{
$feeds = new Feed_Amalgamator;
$feeds->addFeeds( $urls );
$feeds->grabRss();
$feeds->amalgamate();
$feeds_pl = new Feed_Amalgamator;
$feeds_pl->addFeeds( $urls_pl );
$feeds_pl->grabRss();
$feeds_pl->amalgamate();
}
catch ( exception $e )
{
die( $e->getMessage() );
}
?>
<div style="border: 1px solid black; background-color: #CCCCCC; padding: 5px;">
<a id="myHeader1" href="javascript:showonlyone('newboxes1');" ><img src="http://www.ligamistrzow.com/img/kraje/1.png" height="10px"> POLSKA </a>
</div>
<div style="border: 1px solid black; background-color: #CCCCCC; padding: 5px;">
<a id="myHeader2" href="javascript:showonlyone('newboxes2');" ><img src="http://www.mricons.com/store/png/114777_32086_64_explorer_globe_internet_icon.png" height="15px"> ZAGRANICA</a>
</div>
<div class="newboxes" id="newboxes1" style="border: 1px solid black; background-color: white; display: none;padding: 5px;">
<?php foreach ( $feeds_pl->data as $item ) : extract( (array) $item);?>
<li><a href="<?php echo $link; ?>" target="_blank"><?php echo $title; ?></a> <font size="1"><?php echo $pubDate ?></font></br>
<?php endforeach; ?></div>
<div class="newboxes" id="newboxes2" style="border: 1px solid black; background-color: white; display: none;padding: 5px;">
<?php foreach ( $feeds->data as $item) : extract( (array) $item);?>
<li><a href="<?php echo $link; ?>" target="_blank"><?php echo $title; ?></a> <font size="1"><?php echo $pubDate ?></font></br>
<?php endforeach; ?></div>
</div>
FEEDS.PHP文件
<script type="text/javascript">
function showonlyone(thechosenone) {
var newboxes = document.getElementsByTagName("div");
for(var x=0; x<newboxes.length; x++) {
name = newboxes[x].getAttribute("class");
if (name == 'newboxes') {
if (newboxes[x].id == thechosenone) {
newboxes[x].style.display = 'block';
}
else {
newboxes[x].style.display = 'none';
}
}
}
}
</script>
<?php
function get_title()
{
if ($this->title !== null)
{
return $this->title;
}
else
{
return null;
}
}
class Feed_Amalgamator
{
public $urls = array();
public $data = array();
public function addFeeds( array $feeds )
{
$this->urls = array_merge( $this->urls, array_values($feeds) );
}
public function grabRss()
{
foreach ( $this->urls as $feed )
{
$data = @new SimpleXMLElement( $feed, 0, true );
if ( !$data )
throw new Exception( 'Could not load: ' . $feed );
foreach ( $data->channel->item as $item )
{
$this->data[] = $item;
}
}
}
public function amalgamate()
{
shuffle( $this->data );
$temp = array();
foreach ( $this->data as $item )
{
if ( !in_array($item->link, $this->links($temp)) )
{
$temp[] = $item;
}
}
$this->data = $temp;
shuffle( $this->data );
}
private function links( array $items )
{
$links = array();
foreach ( $items as $item )
{
$links[] = $item->link;
}
return $links;
}
}
?>
答案 0 :(得分:0)
某些东西没有被正确转义。对于XML,基本上无效的字符为<
>
&
和"
,但这可能不是完整的列表。
在没有对其进行任何处理的情况下,您无法在很大程度上信任来自外部来源的数据,甚至是内部来源的数据。
您可以使用http://php.net/htmlspecialchars来转义字符串,然后再将其输出为XML:
foreach($data->channel->item as $item){
$this->data[] = htmlspecialchars($item);
}
我希望这会有所帮助。