0

0

如何用PHP把RDF内容插入Web站点之中(四)

php中文网

php中文网

发布时间:2016-06-21 09:13:57

|

1630人浏览过

|

来源于php中文网

原创

web|插入|站点

返回到类(Back To Class)

既然你有这么大的权力,那么究竟为什么要把自己限制在仅仅是单个的RDF来源呢?就象我早先说过的一样,大多数主要的站点都经常为他们所提供的内容做快照。其实将所有这些不同的来源插入到你的站点当中是相当简单的。让我们看看是如何做的。

首先,我们把前面例子中的代码模块化。这样一来,你就无须为每一个单个的来源都一遍又一遍的重写相同的代码了。简化的方法就是将之打包成类,再把这个类包含到我的PHP脚本当中。

类代码如下:


class RDFParser
{
//
// variables
//

// set up local variables for this class
var $currentTag = "";
var $flag = "";
var $count = 0;

// this is an associative array of channel data with keys
("title", "link", "description")
var $channel = array();

// this is an array of arrays, with each array element
representing an
// each outer array element is itself an associative array
// with keys ("title", "link", "description")
var $items = array();


//
// methods
//

// set the name of the RDF file to parse
// this is usually a local file
// you may set it to a remote file if your PHP build supports
URL fopen()
function setResource($file)
{
$this->file = $file;
}


// parse the RDF file set with setResource()
// this populates the $channel and $items arrays
function parseResource()
{
// create parser
$this->xp = xml_parser_create();

// set object reference
xml_set_object($this->xp, $this);

// set handlers and parser options
xml_set_element_handler($this->xp, "elementBegin",
"elementEnd");
xml_set_character_data_handler($this->xp,
"characterData");
xml_parser_set_option($this->xp,
XML_OPTION_CASE_FOLDING, TRUE);
xml_parser_set_option($this->xp, XML_OPTION_SKIP_WHITE,
TRUE);

// read XML file
if (!($fp = fopen($this->file, "r")))
{
die("Could not read $this->file");
}

// parse data
while ($xml = fread($fp, 4096))
{
if (!xml_parse($this->xp, $xml, feof($fp)))
{
die("XML parser error: " .
xml_error_string(xml_get_error_code($this->xp)));
}
}

// destroy parser
xml_parser_free($this->xp);
}

// opening tag handler
function elementBegin($parser, $name, $attributes)
{
$this->currentTag = $name;
// set flag if entering or block
if ($name == "ITEM")
{
$this->flag = 1;
}
else if ($name == "CHANNEL")
{
$this->flag = 2;
}
}

// closing tag handler
function elementEnd($parser, $name)
{
$this->currentTag = "";

// set flag if exiting or block
if ($name == "ITEM")
{
$this->count++;
$this->flag = 0;
}
else if ($name == "CHANNEL")
{
$this->flag = 0;
}
}

// character data handler
function characterData($parser, $data)
{
$data = trim(htmlspecialchars($data));
if ($this->currentTag == "TITLE" || $this->currentTag ==
"LINK" || $this->currentTag == "DESCRIPTION")
{
// add data to $channels[] or $items[] array
if ($this->flag == 1)
{

$this->items[$this->count][strtolower($this->currentTag)] .= $data;
}
else if ($this->flag == 2)
{

$this->channel[strtolower($this->currentTag)] .= $data;
}
}
}

// return an associative array containing channel information
// (the $channel[] array)
function getChannelInfo()
{
return $this->channel;
}

// return an associative array of arrays containing item
information
// (the $items[] array)
function getItems()
{
return $this->items;
}

}
?>
如果你对PHP类较为熟悉的话,那么理解这段代码是相当容易的。如果不太懂的话,那么请直接跳到文章末尾的链接部分,看一篇关于类工作原理的好文章。然后在回来继续阅读上面的代码。

在使用这个类之前,我要特别花几分钟指出其中的一行代码——即上面对xml_set_object()函数调用的那一行。

现在的问题是如何使用这个类实际生成具有多个内容来源的Web页。


include("class.RDFParser.php");
// how many items to display in each channel
$maxItems = 5;
?>



















// get and parse freshmeat.net channel
$f = new RDFParser();
$f->setResource("http://www.freshmeat.net/backend/fm-releases.rdf");
$f->parseResource();
$f_channel = $f->getChannelInfo();
$f_items = $f->getItems();
// now format and print it...
?>
The latest from > echo
$f_channel["title"]; ?>

    // iterate through items array
    for ($x=0; $x if (is_array($f_items[$x]))
    {
    // print data
    $item = $f_items[$x];
    echo "
  • " .
    $item["title"] . "
    ";
    }
    }
    ?>



Primary page content here



// get and parse slashdot.org channel
$s = new RDFParser();
$s->setResource("http://slashdot.org/slashdot.rdf");
$s->parseResource();
$s_channel = $s->getChannelInfo();
$s_items = $s->getItems();
// now format and print it...
?>
The latest from > echo
$s_channel["title"]; ?>

    // iterate through items array
    for ($x=0; $x if (is_array($s_items[$x]))
    {
    // print data
    $item = $s_items[$x];
    echo "
  • " .
    $item["title"] . "
    ";
    }
    }
    ?>









这段代码相当简单。一旦你用“new”关键字生成一个类的实例,

$f = new RDFParser();

那么就可以用类方法来设置要分析的RDF文件的位置,

$f->setResource("http://www.freshmeat.net/backend/fm-releases.rdf");
$f->parseResource();
并且获取$channel和$items数组,以用于后面的处理。




$f_channel = $f->getChannelInfo();
$f_items = $f->getItems();
?>

The latest from > echo
$f_channel["title"]; ?>

    // iterate through items array
    for ($x=0; $x if (is_array($f_items[$x]))
    {
    // print data
    $item = $f_items[$x];
    echo "
  • " .
    $item["title"] . "
    ";
    }
    }
    ?>



每次你重新装入上面的脚本,相应的RDF文件就会被从特定的位置上取来,经过分析之后,按要求的格式显示出来。

如果你站点具有高的访问量,你就可能觉得我们的辛苦无意义之极,尤其是当所用的RDF数据更新地没有那么快时,情况更糟。 在这种情况下,或许探究一下在本地缓存RDF数据才是较明智的做法:要么扩展上面的例子程序,在其中加入缓存功能;要么每阁几个小时都花很长的时间下载一个最新RDF文件的本地副本到你的Web服务器上,然后使用这个本地副本,而不是那个“活”的(the “live” one)。



B站看视频
B站看视频

B站汇集了丰富的热门视频资源,海内外流量爆款的这里都有,最新最热的视频资源第一时间奉上,有需要的小伙伴快来保存下载体验吧!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

32

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

23

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

16

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

5

2026.01.31

Golang人工智能合集
Golang人工智能合集

本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

6

2026.01.31

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

268

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

195

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

170

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

85

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 15万人学习

Pandas 教程
Pandas 教程

共15课时 | 1万人学习

C# 教程
C# 教程

共94课时 | 8.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号