|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
算是1个熟悉的过程,所以c语言的基础有就更好,没有也不怕。rss|新浪|雅虎|站点 [媒介]
在团体建站的过程当中,常常要从其他网站获得大批静态信息。
本文所描写的就是利用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 的解gh境</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 的解gh境<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.pushad.com/XrssFile/2007-1/30/2007130142039121.gif'>
<!--
<img src='http://www.pushad.com/XrssFile/2007-1/30/2007130142039669.gif'>
<img src='http://jlinux.ddo.jp/bbs/images/default/logo.gif'>
<img src='http://www.pushad.com/XrssFile/2007-1/30/2007130142039970.gif'>
//-->
</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>
</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__;
?>
最近陆续的有人问我学习php的心得,现在整理为下面,希望可以对大家有些帮助。 |
|