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
>
查看全文
相关阅读:
python远程连接服务器并查看服务器上的文件
python中xe6x80xa5xe8xafx8a如何转为中文
idea+testng+maven环境搭建
Django restfulframework系列
django-基于函数的视图与基于类的视图
yaml结合数据驱动编写测试用例
pycharm社区版找不到工具栏找不到Database的解决办法
Content-Type 对照表
ffmpeg C# 切第一秒的有图片
Angular HttpClient POST 服务器(springboot)获取不到参数问题
原文地址:https://www.cnblogs.com/lang/p/995384.html
最新文章
Django项目部署
python的自省,如type(),dir(),getattr(),hasattr(),isinstance().
python的单例模式(转)
pycharm专业版开发django项目时遇到的问题
django系列笔记
mock的使用-解决接口依赖问题
pytest插件的理解
python内置函数eval()在测试脚本断言中的应用
python-数据写入json文件时如何保证格式美观且中文正常显示?
python-pprint()使输出更美观易读
热门文章
python-enumerate() 函数的用法
白盒测试
selenium-常用webdriver API
python-多任务之进程、线程、协程之间的区别与联系
算法的时间复杂度和空间复杂度
leetcode刷题计划
pytest标记测试用例
线性表
测试脚本汇总
python模块-pymysql源码分析及其常见使用
Copyright © 2011-2022 走看看