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
>
查看全文
相关阅读:
[译]The multi Interface
[译]libcurl_tutorial
[译]curl_multi_perform
[译]curl_multi_info_read
[译]libcurl错误码
YumRepo Error: All mirror URLs are not using ftp, http[s] or file
linux 修改文件用户组和所有者
linux禁止root用户直接登录sshd并修改默认端口
修改root登录用户名减少阿里云Linux主机“被暴力破解”警告
CentOS云主机安全之新增ssh登录账户、禁止ROOT登陆
原文地址:https://www.cnblogs.com/lang/p/995384.html
最新文章
Android UI 绘制过程浅析(一)LayoutInflater简介
[EventBus源码解析] EventBus.post 方法详述
[EventBus源码解析] EventBus.register 方法详述
[EventBus源码解析] 初探EventBus
记一次Android内存分析过程
魅族手机(魅蓝note)无法作为调试设备连接到mac问题的解决
OS X 禁止Android File Transfer自动启动
【git】分支合并
【php】fpm的一些异常记录
【php】php-fpm的中断和启动控制
热门文章
19-11 【新闻:gitlab放弃mysql,仅支持PostgreSQL】
【php】查看扩展的版本
19-09 【github 自动提取项目的topic】
19-08 【安全相关】
19-07 【docker】随笔笔记
19-06 【phpunit和docker】
19-05【icloud】照片备份
[译]Customizing Operations
[译]Cookies Without Chocolate Chips
[译]HTTP POSTing
Copyright © 2011-2022 走看看