zoukankan
html css js c++ java
PHP 读RSS
直接运行就OK了。
RSS.CLASS.PHP
<?
php
class
ReadRSS
{
//
Define var as ReadRSS Class property
var
$url
;
var
$content
;
var
$values
;
//
Define construct function
function
ReadRSS(
$url
)
{
$this
->
url
=
$url
;
}
//
This is the Function to get file content.(read file)
function
ReadFile
()
{
$fp
=
fopen
(
$this
->
url
,
"
r
"
);
while
(
!
feof
(
$fp
) )
{
$this
->
content
.=
fgets
(
$fp
,
4096
);
}
fclose
(
$fp
);
}
//
Read Xml File
function
ReadXML(){
$parser
=
xml_parser_create
();
//
简历xml解析器
xml_parser_set_option
(
$parser
,
XML_OPTION_SKIP_WHITE
,
1
);
//
忽略由空白字符组成的值
xml_parser_set_option
(
$parser
,
XML_OPTION_TARGET_ENCODING
,
'
UTF-8
'
);
//
设置编码,目前只支持ISO-8859-1、US-ASCII 和 UTF-8,也可以不设置,与目标编码一致
xml_parse_into_struct
(
$parser
,
$this
->
content
,
$this
->
values);
xml_parser_free
(
$parser
);
}
function
RSS(
$n
=
10
){
$this
->
ReadFile
();
$this
->
ReadXML();
$in_item
=
0
;
$i
=
0
;
$read
=
array
();
foreach
(
$this
->
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
"
){
$read
[
$i
][
'
link
'
]
=
$link
;
$read
[
$i
][
'
title
'
]
=
$title
;
if
(
$pubDate
) {
$read
[
$i
][
'
pubDate
'
]
=
$pubDate
;
}
$read
[
$i
][
'
description
'
]
=
$description
;
$in_item
=
0
;
$i
++
;
if
(
$i
>=
$n
) {
break
;
}
}
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
;
}
}
}
return
$read
;
}
}
?>
RSS.PHP
<
html
>
<
head
>
<
meta http
-
equiv
=
'
content-type
'
content
=
'
text/html; charset=UTF-8
'
>
<
title
>
RSS_TEST
</
title
>
</
head
>
<
body
>
<?
php
include_once
(
'
rss.class.php
'
);
$url
=
"
http://dudu.cnblogs.com/rss.aspx
"
;
$rss
=
new
ReadRSS(
$url
);
$values
=
$rss
->
RSS(
10
);
foreach
(
$values
as
$value
) {
echo
"
<div><a href={$value['link']}>{$value['title']}</a></div>
"
;
}
?>
</
body
>
</
html
>
查看全文
相关阅读:
hdu4277 暴力
hdu4271 Find Black Hand 2012长春网络赛E题 最短编辑距离
poj3356 字符串的最小编辑距离 dp
HDU4267 A Simple Problem with Integers 线段树/树状数组
树链剖分 模版
SPOJ375 Query on a tree 树链剖分
Orz_panda cup I题 (xdoj1117) 状压dp
27号的十道离线线段树
The 2015 "Orz Panda" Cup Programming Contest
codeforces #274 C. Riding in a Lift dp+前缀和优化
原文地址:https://www.cnblogs.com/lang/p/995384.html
最新文章
同余定理 专题
素数 专题
拓展欧几里得 专题
poj 3311 经典tsp问题,状压dp
UVa 714
二分·归并排序与树状数组之逆序对 hiho1141
网络流 小结(更新时间2015/8/8)更新中
树形dp hdu 2196
树形dp poj 2342
欧拉路径 提高篇 hiho第51周
热门文章
codeforces round #321 (div2)
AC自动机专题
9月22号~25号
2015沈阳网络赛1003 Minimum Cut 树链剖分 数组维护前缀和进行区间增减
codeforces#320(div2) E. Weakness and Poorness 三分
codeforces#320(div2) D "Or" Game 贪心
codeforces#320(div2) C A Problem about Polyline 二分
xdoj1012 字符串哈希
2015长春网络赛总结
HDU5029 Relief grain 树链剖分+线段树离线
Copyright © 2011-2022 走看看