读取站点更新纪录(RSS2.0) - php篇 :新浪,雅虎新闻,csdn blog...

2024-02-16 20:50

本文主要是介绍读取站点更新纪录(RSS2.0) - php篇 :新浪,雅虎新闻,csdn blog...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文链接地址:http://blog.csdn.net/thefirstwind/archive/2007/01/18/1486536.aspx
作者:邢晓宁
时间:2007年1月18日
声明:版权没有,随你任转

[前言]
在个人建站的过程中,经常要从其他网站获取大量动态信息。 
本文所描述的就是使用php程序读取rss标准的xml格式文件,动态显示他人站点的信息列表。

[演示]

Yahoo News : perl php Perl/PHP XML::RSS读Yahoo新闻(英文)的例子
My CSDN Blog : perl php Perl/PHP XML::RSS读取个人CSDN博客的例子
JLinux : perl php Perl/PHP XML::RSS读取JLinux的例子
新浪新闻 综合 perl php 体育 perl php 娱乐 perl php

 

[前提]
对于php编程爱好者来说,前期的准备相对简单,只要有php4以上的环境就可以建立此功能。

[对应的XML/RSS文件的格式]
基本上很多网站提供的用来做rss浏览的文件都是以下的格式,这是符合xml的w3c通用标准的。
简单的分析一下,
基本的树结构是,
一个rss根下,有一个channel节点,
  该channel节点下的title,link,description属性是常用的,
     然后就是item节点,众多item节点是最近跟新的若干篇文章,
    该item节点下的title,link,pubDate,description属性是常用的。
   简单格式如下:

< 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 >
< title > 本站点频道的标题 </ title >
< link > 链接地址 </ link >
< description > 站点频道描述信息 </ description >
< item >
< title > 文章1 </ title >
< link > 文章1链接地址 </ link >
< description > 文章1内容简介 </ description >
</ item >
< item >
< title > 文章2 </ title >
< link > 文章2链接地址 </ link >
< description > 文章2内容简介 </ description >
</ item >
</ channel >
</ rss >




举例:

< 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 >
  
< title > 邢晓宁专栏 </ title >  
  
< link > http://blog.csdn.net/thefirstwind/ </ link >  
  
< description > 代码一生 </ description >  
  
< dc:language > af </ dc:language >  
  
< generator > .Text Version 1.0.1.1 </ generator >  
  
< image > http://counter.csdn.net/pv.aspx?id=72 </ image >  
< item >
  
< dc:creator > ♂猜猜♂(邢晓宁) </ dc:creator >  
  
< title > 在 MS Windows 下建立 DocBook 的解譯環境 </ title >  
  
< link > http://blog.csdn.net/thefirstwind/archive/2006/12/21/1451714.aspx </ link >  
  
< pubDate > Thu, 21 Dec 2006 13:50:00 GMT </ pubDate >  
  
< guid > http://blog.csdn.net/thefirstwind/archive/2006/12/21/1451714.aspx </ guid >  
  
< wfw:comment > http://blog.csdn.net/thefirstwind/comments/1451714.aspx </ wfw:comment >  
  
< comments > http://blog.csdn.net/thefirstwind/archive/2006/12/21/1451714.aspx#Feedback </ comments >  
  
< slash:comments > 0 </ slash:comments >  
  
< wfw:commentRss > http://blog.csdn.net/thefirstwind/comments/commentRss/1451714.aspx </ wfw:commentRss >  
  
< trackback:ping > http://tb.blog.csdn.net/TrackBack.aspx?PostId=1451714 </ trackback:ping >  
  
< description > 在 MS Windows 下建立 DocBook 的解譯環境 < img  src  ="http://blog.csdn.net/thefirstwind/aggbug/1451714.aspx"  width  = "1"  height  = "1"   /></ description >  
  
</ item >
< item >
  
< dc:creator > 邢晓宁 </ dc:creator >  
  
< title > 程序员学习的革命-如何使用大脑 </ title >  
  
< link > http://blog.csdn.net/thefirstwind/archive/2006/12/13/1440965.aspx </ link >  
  
< pubDate > Wed, 13 Dec 2006 09:41:00 GMT </ pubDate >  
  
< guid > http://blog.csdn.net/thefirstwind/archive/2006/12/13/1440965.aspx </ guid >  
  
< wfw:comment > http://blog.csdn.net/thefirstwind/comments/1440965.aspx </ wfw:comment >  
  
< comments > http://blog.csdn.net/thefirstwind/archive/2006/12/13/1440965.aspx#Feedback </ comments >  
  
< slash:comments > 27 </ slash:comments >  
  
< wfw:commentRss > http://blog.csdn.net/thefirstwind/comments/commentRss/1440965.aspx </ wfw:commentRss >  
  
< trackback:ping > http://tb.blog.csdn.net/TrackBack.aspx?PostId=1440965 </ trackback:ping >  
  
< description > 很多人搞技术,还有很多转行搞技术,搞了一段时间终于发现,自己不适合作技术,要我说其实就是用脑方式的问题。真的学会适当的用脑方式,编程编起来得心应手。 < img  src  ="http://blog.csdn.net/thefirstwind/aggbug/1440965.aspx"  width  = "1"  height  = "1"   /></ description >  
  
</ item >
  
</ channel >
  
</ rss >

 

[核心程序]

<? php

$RSSURL   =   " http://blog.csdn.net/thefirstwind/Rss.aspx " ;
$buff   =   "" ;
$fp   =   fopen ( $RSSURL , " r " );
while  (  ! feof ( $fp ) ) {
    
$buff   .=   fgets ( $fp , 4096 );
}
fclose ( $fp );

$parser   =   xml_parser_create ();
xml_parser_set_option ( $parser , XML_OPTION_SKIP_WHITE , 1 );
xml_parse_into_struct ( $parser , $buff , $values , $idx );
xml_parser_free ( $parser );

$in_item   =   0 ;
foreach  ( $values   as   $value ) {
    
$tag    =   $value [ " tag " ];
    
$type   =   $value [ " type " ];
    
$value   =   $value [ " value " ];

    
$tag   =   strtolower ( $tag );
    
if  ( $tag   ==   " item "   &&   $type   ==   " open " ) {
        
$in_item   =   1 ;
    } 
else   if  ( $tag   ==   " item "   &&   $type   ==   " close " ) {
        
echo   <<< EOM
$title
$link
$description
EOM;
        
$in_item   =   0 ;
    }
    
if  ( $in_item ) {
        
switch  ( $tag ) {
            
case   " title " :
                
$title   =   $value ;
                
break ;
            
case   " link " :
                
$link   =   $value ;
                
break ;
            
case   " description " :
                
$description   =   $value ;
                
break ;
        }
    }
}

?>

 

[配合上以上说明,完整的源代码如下]
以下附加了css样式。

<? php

# $RSSURL = "http://www3.asahi.com/rss/index.rdf";
#
$RSSURL = "http://rss.news.yahoo.com/rss/topstories";
$RSSURL   =   " http://blog.csdn.net/thefirstwind/Rss.aspx " ;
# $RSSURL = "http://jlinux.ddo.jp/bbs/rss.php?auth=0";
#
$RSSURL = "http://rss.sina.com.cn/news/marquee/ddt.xml";
#
$RSSURL = "http://rss.sina.com.cn/news/allnews/sports.xml";
#
$RSSURL = "http://rss.sina.com.cn/news/allnews/ent.xml";

$buff   =   "" ;
$fp   =   fopen ( $RSSURL , " r " );
while  (  ! feof ( $fp ) ) {
    
$buff   .=   fgets ( $fp , 4096 );
}
fclose ( $fp );

$parser   =   xml_parser_create ();
xml_parser_set_option ( $parser , XML_OPTION_SKIP_WHITE , 1 );
xml_parse_into_struct ( $parser , $buff , $values , $idx );
xml_parser_free ( $parser );
$channel_title   =   $values [ 2 ][ " value " ];
echo   <<< __HTML__
< html >
< head >
< meta http - equiv = ' content-type '  content = ' text/html; charset=UTF-8 ' >
< title > $channel_title </ title >
< link rel = ' stylesheet '  type = ' text/css '  id = ' css '  href = ' /bbs/forumdata/cache/style_1.css ' >
< script type = ' text/javascript '  src = ' /bbs/include/common.js ' ></ script >
< script type = ' text/javascript '  src = ' /bbs/include/menu.js ' ></ script >
</ head >
< body >

< table border = ' 1 ' >
< tr >< td >
< img src = ' http://www.csdn.net/Images/logo_csdn.gif ' >& nbsp; & nbsp;
<!--
< img src = ' http://us.i1.yimg.com/us.yimg.com/i/us/nt/ma/ma_nws_1.gif ' >& nbsp; & nbsp;
< img src = ' http://jlinux.ddo.jp/bbs/images/default/logo.gif ' >& nbsp; & nbsp;
< img src = ' http://image2.sina.com.cn/dy/sina_news626.gif ' >& nbsp; & nbsp;
// -->
</ td >
< td >
$channel_title
$channel_lastBuildDate < br >
</ td >
</ td >
__HTML__;

$in_item   =   0 ;
foreach  ( $values   as   $value ) {
    
$tag    =   $value [ " tag " ];
    
$type   =   $value [ " type " ];
    
$value   =   $value [ " value " ];

    
$tag   =   strtolower ( $tag );
    
if  ( $tag   ==   " item "   &&   $type   ==   " open " ) {
        
$in_item   =   1 ;
    } 
else   if  ( $tag   ==   " item "   &&   $type   ==   " close " ) {
        
echo   <<< EOM
< tr >
  
< td colspan = ' 2 '   class = ' header ' width = ' 400 ' >
    
< a href = " $link " > $title </ a >
  
</ td >
</ tr >
< tr >
  
< td colspan = ' 2 '  width = ' 400 ' align = ' right ' >
    
$pubDate
  
</ td >
</ tr >
< tr >
  
< td colspan = ' 2 '  width = ' 400 ' >
    
$description
  
</ td >
</ tr >
< tr >
  
< td >
    
& nbsp;
  
</ td >
</ tr >
EOM;
        
$in_item   =   0 ;
    }
    
if  ( $in_item ) {
        
switch  ( $tag ) {
            
case   " title " :
                
$title   =   $value ;
                
break ;
            
case   " link " :
                
$link   =   $value ;
                
break ;
            
case   " pubDate " :
                
$pubDate   =   $value ;
                
break ;
            
case   " description " :
                
$description   =   $value ;
                
break ;
        }
    }
}

echo   <<<  __HTMLEND__
</ table >
</ body >
</ html >
__HTMLEND__;

?>
                        

这篇关于读取站点更新纪录(RSS2.0) - php篇 :新浪,雅虎新闻,csdn blog...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/715672

相关文章

MySQL新增字段后Java实体未更新的潜在问题与解决方案

《MySQL新增字段后Java实体未更新的潜在问题与解决方案》在Java+MySQL的开发中,我们通常使用ORM框架来映射数据库表与Java对象,但有时候,数据库表结构变更(如新增字段)后,开发人员可... 目录引言1. 问题背景:数据库与 Java 实体不同步1.1 常见场景1.2 示例代码2. 不同操作

解决Java中基于GeoTools的Shapefile读取乱码的问题

《解决Java中基于GeoTools的Shapefile读取乱码的问题》本文主要讨论了在使用Java编程语言进行地理信息数据解析时遇到的Shapefile属性信息乱码问题,以及根据不同的编码设置进行属... 目录前言1、Shapefile属性字段编码的情况:一、Shp文件常见的字符集编码1、System编码

一文详解SQL Server如何跟踪自动统计信息更新

《一文详解SQLServer如何跟踪自动统计信息更新》SQLServer数据库中,我们都清楚统计信息对于优化器来说非常重要,所以本文就来和大家简单聊一聊SQLServer如何跟踪自动统计信息更新吧... SQL Server数据库中,我们都清楚统计信息对于优化器来说非常重要。一般情况下,我们会开启"自动更新

利用Python实现添加或读取Excel公式

《利用Python实现添加或读取Excel公式》Excel公式是数据处理的核心工具,从简单的加减运算到复杂的逻辑判断,掌握基础语法是高效工作的起点,下面我们就来看看如何使用Python进行Excel公... 目录python Excel 库安装Python 在 Excel 中添加公式/函数Python 读取

Python如何实现读取csv文件时忽略文件的编码格式

《Python如何实现读取csv文件时忽略文件的编码格式》我们再日常读取csv文件的时候经常会发现csv文件的格式有多种,所以这篇文章为大家介绍了Python如何实现读取csv文件时忽略文件的编码格式... 目录1、背景介绍2、库的安装3、核心代码4、完整代码1、背景介绍我们再日常读取csv文件的时候经常

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

Redis缓存问题与缓存更新机制详解

《Redis缓存问题与缓存更新机制详解》本文主要介绍了缓存问题及其解决方案,包括缓存穿透、缓存击穿、缓存雪崩等问题的成因以及相应的预防和解决方法,同时,还详细探讨了缓存更新机制,包括不同情况下的缓存更... 目录一、缓存问题1.1 缓存穿透1.1.1 问题来源1.1.2 解决方案1.2 缓存击穿1.2.1

Linux Mint Xia 22.1重磅发布: 重要更新一览

《LinuxMintXia22.1重磅发布:重要更新一览》Beta版LinuxMint“Xia”22.1发布,新版本基于Ubuntu24.04,内核版本为Linux6.8,这... linux Mint 22.1「Xia」正式发布啦!这次更新带来了诸多优化和改进,进一步巩固了 Mint 在 Linux 桌面

Java读取InfluxDB数据库的方法详解

《Java读取InfluxDB数据库的方法详解》本文介绍基于Java语言,读取InfluxDB数据库的方法,包括读取InfluxDB的所有数据库,以及指定数据库中的measurement、field、... 首先,创建一个Java项目,用于撰写代码。接下来,配置所需要的依赖;这里我们就选择可用于与Infl

SpringCloud配置动态更新原理解析

《SpringCloud配置动态更新原理解析》在微服务架构的浩瀚星海中,服务配置的动态更新如同魔法一般,能够让应用在不重启的情况下,实时响应配置的变更,SpringCloud作为微服务架构中的佼佼者,... 目录一、SpringBoot、Cloud配置的读取二、SpringCloud配置动态刷新三、更新@R