字符串不能是[..]RSS阅读器在PHP


String could not be [...] RSS Reader in PHP

目前我正在为PHP开发RSS阅读器。我快完成了,但是我遇到了一些问题。这个问题不时出人意料地出现。有时我有文本"字符串不能解析为XML"。我认为这是因为一些RSS频道在PHP中显示有问题。当我发现并最终删除有问题的通道时,问题消失了,但当我再次添加它时,问题也没有出现。看起来像是某种时间误差。我不知道。

你有什么建议吗?

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

没有正确转义。基本上无效的字符是<>&",尽管这可能不是一个完整的列表。

你不能从本质上信任外部来源的数据,甚至在很多时候不做任何处理就信任内部来源的数据。

您可以使用http://php.net/htmlspecialchars在将字符串输出为XML之前对其进行转义:

foreach($data->channel->item as $item){
  $this->data[] = htmlspecialchars($item);
}