盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

怎么用PHP把RDF内容插入Web网站之中(4)

时间:2025/1/18作者:未知来源:盾怪网教程人气:

[摘要]this->currentTag == "DESCRIPTION") // add data to channel...
$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;
?>
<html>
<head>
<basefont face="Verdana">
<body>

<table width="100%" border="0" cellspacing="5" cellpadding="5"> <tr>
<!-- first cell -->
<td valign=top align=left>
<font size="-1">
<?
// 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 <a href=<? echo $f_channel["link"]; ?>><? echo
$f_channel["title"]; ?></a> <br> <ul> <? // iterate through items array
for ($x=0; $x<$maxItems; $x++) {
if (is_array($f_items[$x]))
{
// print data
$item = $f_items[$x];
echo "<li><a href=" . $item["link"] . ">" .
$item["title"] . "</a>";
}
}
?>
</ul>
</font>
</td>

<!-- second cell -->
<td align=center width=50%>
<i>Primary page content here</i>
</td>

<!-- third cell -->
<td valign=top align=left>
<font size="-1">
<?
// 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 <a href=<? echo $s_channel["link"]; ?>><? echo
$s_channel["title"]; ?></a> <br> <ul> <? // iterate through items array
for ($x=0; $x<$maxItems; $x++) {
if (is_array($s_items[$x]))
{
// print data
$item = $s_items[$x];
echo "<li><a href=" . $item["link"] . ">" .
$item["title"] . "</a>";
}
}
?>
</ul>
</font>
</td>

</tr>


</body>
</head>
</html>


这段代码相当简单。一旦你用“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 <a href=<? echo $f_channel["link"]; ?>><? echo
$f_channel["title"]; ?></a> <br> <ul> <? // iterate through items array
for ($x=0; $x<$maxItems; $x++) {
if (is_array($f_items[$x]))
{
// print data
$item = $f_items[$x];
echo "<li><a href=" . $item["link"] . ">" .
$item["title"] . "</a>";
}
}
?>
</ul>


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

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



关键词:如何用PHP把RDF内容插入Web网站之中(4)




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版