|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
那么接下来,这就算学会啦?NO,NO,NO,还早呢,你至尽还没碰过OOP之类的吧?模板呢?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 <item>
// 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 <channel> or <item> 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 <channel> or <item> 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;
?>
<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>
</table>
</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)。
不可能吃饭的时候咬了自己一下舌头就从此不吃饭了不是?放下畏惧,继续努力,咱们是来征服它的,而不是被它征服的,振奋起来吧同志。 |
|