一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php rss分析类程序

时间:2010-03-12 编辑:简简单单 来源:一聚教程网

class rss74 {

    // RSS feed title:
    var $title = "Untitled";
   
    // RSS description:
    var $desc = "";
   
    // RSS base url
    // -> Example: http://www.jonasjohn.de/
    var $base_url = "";
   
    // XSL file for the resulting RSS feed:
    var $xsl_file = 'rss.xsl';
   
    // RSS 2.0 Specification (URL):
    var $doc_url = 'http://blogs.law.harvard.edu/tech/rss';
   
    // Copyright text:
    // -> Example: Copyright 2006, Jonas John
    var $copyright = '';
   
    // RSS language setting:
    // Example: en-us, de-de, fr-fr
    var $language = 'en-us';
   
    // Managing editor and webmaster:
    // (should contain a E-Mail adress)
    var $managing_editor = '';
    var $webmaster = '';
   
    // Feedburner URL
    // Example: http://feeds.feedburner.com/codedump-rss
    // (If a FB URL is set, all requests except the Feedburner and Google
    // requests will be redricted to the feedburner URL)
    var $feedburner_url = '';
   
    // RSS generator:
    var $generator = 'rss74/v0.3';
   
    // Limit RSS entries to:
    // (Example: 20, 30, 40, 50, etc.)
    var $limit_entries = 20;

    // RSS items:
    var $items = array();

    // constructor:
    function rss74(){
    }  
   
    function add_entry($entry){   
   
        // create date key:
        $date = isset($entry['date']) ? $entry['date'] : time();  

        // add unique string:
        $date .= '_' . md5($entry['title']);
       
        $this->items[$date] = $entry;
    }
   
    function _get_val(&$array, $key){
        return isset($array[$key]) ? $array[$key] : '';
    }
   
    function _exists_val(&$array, $key){
        return isset($array[$key]);
    }
   
    function get_rss_headers($rss_webpath){
        return '';
    }
   
    function print_rss(){
        global $_SERVER;
   
        krsort($this->items);
        $this->items = array_slice($this->items, 0, $this->limit_entries);
       
        $first_item = array_keys($this->items);
        $first_item = $first_item[0];
       
        $last_change = $this->items[$first_item]['date'];
        header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_change).' GMT');
       
        header('Content-Type: text/xml; charset=utf-8');

        if (!empty($this->feedburner_url)){
            if (!preg_match("/feedburner/i", $_SERVER['HTTP_USER_AGENT']) and !preg_match("/google/i", $_SERVER['HTTP_USER_AGENT'])) {
                header('HTTP/1.1 301 Moved Permanently');
                header('Location: ' . $this->feedburner_url);
                print 'Redirecting...';
                return true;
            }
        }

        print '' . " ";
       
        if ($this->xsl_file != '')
            print '' . " ";
       
        print ']>' . " ";
       
        print "";
       
        print '' . " ";
        print '' . " ";
       
        print " ".''.htmlentities($this->title).'' . " ";
       
        if (!empty($this->desc))
            print " ".''.htmlentities($this->desc).'' . " ";
       
        if (!empty($this->base_url))
            print " ".''.htmlentities($this->base_url).'' . " ";
       
        print " ".''.date('r', time()).'' . " ";
        print " ".''.date('r', time()).'' . " ";
       
        if (!empty($this->generator))
            print " ".''.$this->generator.'' . " ";
       
        if (!empty($this->copyright))
            print " ".''.$this->copyright.'' . " ";
           
        if (!empty($this->doc_url))
            print " ".''.$this->doc_url.'' . " ";
       
        if (!empty($this->language))
            print " ".$this->language." ";
           
        if (!empty($this->managing_editor))
            print " ".$this->managing_editor." ";

        if (!empty($this->webmaster))
            print " ".$this->webmaster." ";


        while(list($num, $item) = each($this->items)){
     
            print " ";
            print " ".htmlentities($this->_get_val($item, 'title'))." ";
           
            if ($this->_exists_val($item, 'url')){
                print " ".$this->_get_val($item, 'url')." ";
                print " ".$this->_get_val($item, 'url')." ";
            }
           
            if ($this->_exists_val($item, 'desc'))
                print " _get_val($item, 'desc')."]]> ";
           
            if ($this->_exists_val($item, 'date')){
                print " ".date('r', intval($this->_get_val($item, 'date')))." ";
            }
       
            $cats = array();
            while(list($cn, $citem) = each($cats)){
                print " $citem ";
            }
               
            print "
";
            
        }

        print " ";
        print "";
       
        return true;
       
    }
}

实例

 include('inc.rss74.php');

    // RSS items list:
    $example_list = array();   
       
    /*
    ** Create some test entries:
    */
   
    $m = rand(8, 30);
    for ($x = 0; $x < $m; $x++){
   
        // create a random UNIX-Timestamp:
        $date = rand(1166000000, 1166400000);
   
        $example_list[] = array(
            'title' => 'Example RSS message #' . $x,
            'url' => 'http://www.jonasjohn.de/#' . $x,
            'desc' => 'This is a example message from RSS74!',
            'date' => $date
        );
   
    }
   
   
    // create new RSS object:
    $rss = new rss74();
   
    /*
    ** Set RSS informations:
    */
   
    // RSS title:
    $rss->title = 'RSS example 2';
   
    // RSS description:
    $rss->desc = 'This feed shows some random entries.';
   
    // base URL of your homepage:
    $rss->base_url = 'http://www.example.org/';
   
    // limit entry count to 20
    $rss->limit_entries = 20;
   
    // RSS Editor
    $rss->managing_editor = 'Yourname ';

    // Webmaster name
    $rss->webmaster = 'Yourname ';
   
    // language:
    $rss->language = 'en-us';
   
    // Copyright message:
    $rss->copyright = 'Copyright 2006, Yourname';
   
    // Set Feedburner adress:
    //$rss->feedburner_url = 'http://feeds.feedburner.com/codedump-rss';
    // (empty) = No redirection

    // Set "xsl_file" to empty to disable the XSL file:
    $rss->xsl_file = '';

    // Add entries to the RSS object:
    while (list($date, $entry) = each($example_list)){
              
        $rss->add_entry(array(
            'title'     => $entry['title'],
            'url'       => $entry['url'],
            'desc'      => $entry['desc'],
            'date'      => $entry['date']
        ));
   
    }
   
    // let rss74 do the rest:
    $rss->print_rss();

?>

 

热门栏目