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
>
查看全文
相关阅读:
映射文件中增删改查标签中的parameterType和resultType
占位符#{}和拼接符${}
mybatis与hibernate不同(重要)
Mybatis解决jdbc编程的问题
mybatis框架入门程序:演示通过mybatis实现数据库的修改操作
mybatis框架入门程序:演示通过mybatis实现数据库的删除操作
mybatis框架入门程序:演示通过mybatis实现数据库的插入操作中实现返回结果的获取
mybatis框架入门程序:演示通过mybatis实现数据库的添加操作
mybatis框架入门程序:演示通过mybatis实现数据库的模糊查询操作
1047B_Cover Points
原文地址:https://www.cnblogs.com/lang/p/995384.html
最新文章
spring mvc实现登录+异常
spring mvc实现修改+删除
spring mvc实现查询
spring mvc实现新增用户
ssh中getHibernateTemplate()的方法
oracle建表
ssh框架整合完整版
spingMVC<1>-xml文件配置
拆分Spring配置文件
docker容器
热门文章
docker互联
在win10中运行docker
yii2 场景使用
laravel环境安装
python时间模块
django常用命令
django上传文件
yii2模型
python 数据模型orm
查询测试程序中的selectOne和selectList函数
Copyright © 2011-2022 走看看