RSS也是XML,远程读取->XML->分析,看看相关的函数自己写个专门的类来用不更好吗?

解决方案 »

  1.   

    还要怎么详细呢?
    MagpieRSS就是一个读取rss的php类,都给你做好了如果你想自己写,那么应先找到各种rss版本的规范,然后编程解析xml文档。
    当然也可以参考MagpieRSS
      

  2.   

    Rss其实很简单,为什么不拿规范来看看呢?
      

  3.   

    <?php
    /*************************************************
    * 本单元写于 :
    * 作者 : 李腾秋
    * E-Mail : [email protected]
    * 版权所有 
    *************************************************/
    /*************************************************
    作用: RSS生成器
    *************************************************/
    if(!defined(__FILE__))
    {
        define(__FILE__ , 1) ;
    define('RSS_DATE_FORMAT','D, j M Y H:i:s +0800');
    class rss 
    {
        var        $version='2.0';
            var $xml_version='1.0';
            var $channel=array();
            var $encode='utf-8';
            var $source_encode='gb2312';
            var $css='';
            function rss($title,$url,$description=''){
                    $this->channel['title']=$title;
                    $this->channel['url'] = $url;
                    $this->channel['description'] = $description;
                    $this->channel['language']='zh-cn';
                    $this->channel['ttl']='30';
                    $this->channel['generator']='LTQ RSS generator v0.1';
                    $this->channel['pubDate']=date(RSS_DATE_FORMAT);
                    $this->channel['item']=array();
        }
            function setCSS($value)
            {
                    $this->css=$value;
            }
            function set($element, $value)
            {
                    $this->channel[$element]=$value;
            }
            function setImage($title,$url,$link,$width=88,$height=31, $description='')
            {
                    $image = array();
                    $image['title']=$title;
                    $image['url']=$url;
                    $image['link']=$link;
                    if($width > 144) $width=144;
                    $image['width']=$width;
                    if($width > 400) $width=400;
                    $image['height']=$height;
                    $image['description']=$description;                $this->channel['image']=$image;
            }
        function addItem($title,$link,$description,
                    $pubDate='',$author='', $category='', $comments='', $source='')
            {
                    $item=array();
                    $item['title']=$title;
                    $item['link']=$link;
                    $item['description']=$description;
                    if($pubDate!='')
                    {
                            $pubDate=date(RSS_DATE_FORMAT, $pubDate);
                            $item['pubDate']=$pubDate;
                    }
                    if($author!='') $item['author']=$author;
                    if($category!='') $item['category']=$category;
                    if($comments!='') $item['comments']=$comments;
                    if($source!='') $item['source']=$source;                $this->channel['item'][]=$item;
            }
            function _encode()
            {
                    $s = $this->source_encode;
                    $d = $this->encode;
                    if($s==$d) return;
                    if(!function_exists('iconv'))
                    {
                            $this->encode =  $this->source_encode;
                            return ;
                    }
                    $this->__encode($this->channel);
            }
            function __encode( & $data)
            {
                    if(is_string($data))
                    {                 
                            $data=        iconv($this->source_encode, $this->encode, $data);
                            return ;
                    }
                    if(is_array($data))
                    {
                            foreach($data as $k=>$v)
                            {
                                    $this->__encode($data[$k]);
                            }
                            return ;
                    }
            }        function getDoc()
            {
                    $this->_encode();
                    //下面这一个分开为了避免php遇到字符串 < ? ? > 时会出错,
                    $xml='<'.'?xml version="1.0" encoding="'.$this->encode.'"?'.'>';
                    if($this->css!='') $xml .='<?xml-stylesheet type="text/css" href="'.$this->css.'"?>';
                    $xml .='<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
    <channel>';
                    foreach($this->channel as $k=>$v)
                    {
                            if(is_scalar($v))
                                    $xml .="<{$k}>{$v}</{$k}>";
                    }                if(isset($this->channel['image']))
                    {
                            $xml.="<image>";
                            foreach($this->channel['image'] as $k=>$v)
                                    $xml .="<{$k}>{$v}</{$k}>";
                            $xml.="</image>";
                    }                if(count($this->channel['item'])>0)
                    {
                            foreach($this->channel['item'] as $item)
                            {
                                    $xml .= "<item>";
                                    foreach($item as $k=>$v)
                                    {
                                            if($k=='description') $xml .="<description><![CDATA[ {$v} ]]></description>";
                                            else $xml .="<{$k}>{$v}</{$k}>";
                                    }
                                    $xml .= "</item>";
                            }
                    }                $xml.='</channel></rss>';
                    return $xml;
            }
            function SaveToFile($file)
            {
                    $f = @fopen($file,'w');
                    if($f)
                    {
                            fwrite($f, $this->getDoc());
                            fclose($f);
                    }else{
                            exit( "<div>写入文件 {$file} 失败.请确定文件夹存在且可写入.</div>");
                    }
            }}//end class
    }//end if?>
    /*
    $obj=new rss('我的频道','http://www.zazabo.com', '');
    $obj->setImage('图片标题','http://www.zazabo.com/images/logo1.gif','http://www.zazabo.com');
    for($i=0;$i<9;$i++)
            $obj->addItem("标题-[{$i}]", 'http://www.zazabo.com','描述******* [{$i}]');
    $fp = fopen('e:\\mm.xml','w');
    fwrite($fp, $obj->getDoc());
    fclose($fp);*/
      

  4.   

    这个可以<?php
    function my_headlines($url) {
        $rdf = parse_url($url);
        $fp = fsockopen($rdf[ host ], 80, $errno, $errstr, 15);
        if (!$fp) {
            $content = "<font class=\"content\">Problema!</font>";
            return;
        }
        if ($fp) {
            fputs($fp, "GET " . $rdf[ path ] . "?" . $rdf[ query ] . " HTTP/1.0\r\n");
            fputs($fp, "HOST: " . $rdf[ host ] . "\r\n\r\n");
            $string = "";
            while(!feof($fp)) {
         $pagetext = fgets($fp,300);
         $string .= chop($pagetext);
     }
     fputs($fp,"Connection: close\r\n\r\n");
     fclose($fp);
     $items = explode("</item>",$string);
     for ($i=0;$i<10;$i++) {
         $link = ereg_replace(".*<link>","",$items[$i]);
         $link = ereg_replace("</link>.*","",$link);
         $title2 = ereg_replace(".*<title>","",$items[$i]);
         $title2 = ereg_replace("</title>.*","",$title2);
         if ($items[$i] == "") {
             $content = "";
             return;
         } else {
             if (strcmp($link,$title)) {
              $cont = 1;
          $content .= "<li><a href=\"$link\" target=\"_blank\">$title2</a></li>\r\n";
      }
         }
     }
        }
        echo "$content";
    }my_headlines("http://learning.sohu.com/rss/jiaoyuxinwen.xml");
    ?>