麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > PHP > 正文

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

2019-09-08 23:11:37
字體:
來源:轉載
供稿:網友

返回到類(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)
 {
/t   $this->file = $file;
 }

 
 // parse the RDF file set with setResource()
 // this populates the $channel and $items arrays
 function parseResource()
 {
/t   // create parser
/t   $this->xp = xml_parser_create();
/t  
/t   // set object reference
/t   xml_set_object($this->xp, $this);
/t  
/t   // set handlers and parser options
/t   xml_set_element_handler($this->xp, "elementBegin",
"elementEnd");
/t   xml_set_character_data_handler($this->xp,
"characterData");
/t   xml_parser_set_option($this->xp,
XML_OPTION_CASE_FOLDING, TRUE);
/t   xml_parser_set_option($this->xp, XML_OPTION_SKIP_WHITE,
TRUE);
/t  
/t   // read XML file
/t   if (!($fp = fopen($this->file, "r")))
/t   {
/t/t die("Could not read $this->file");
/t   }
/t  
/t   // parse data
/t   while ($xml = fread($fp, 4096))
/t   {
/t       if (!xml_parse($this->xp, $xml, feof($fp)))
/t       {
/t/t/t    die("XML parser error: " .
xml_error_string(xml_get_error_code($this->xp)));
/t       }
/t   }
/t  
/t   // destroy parser
/t   xml_parser_free($this->xp);
 }

 // opening tag handler
 function elementBegin($parser, $name, $attributes)
 {
/t   $this->currentTag = $name;
/t   // set flag if entering <channel> or <item> block
/t   if ($name == "ITEM")
/t   {
/t/t   $this->flag = 1;
/t   }
/t   else if ($name == "CHANNEL")
/t   {
/t/t   $this->flag = 2;
/t   }
 }
 
 // closing tag handler      
 function elementEnd($parser, $name)
 {
/t   $this->currentTag = "";
 
/t   // set flag if exiting <channel> or <item> block
/t   if ($name == "ITEM")
/t   {
/t/t   $this->count++;
/t/t   $this->flag = 0;
/t   }
/t   else if ($name == "CHANNEL")
/t   {
/t/t   $this->flag = 0;
/t   }
 }
 
 // character data handler
 function characterData($parser, $data)
 {
/t   $data = trim(htmlspecialchars($data));
/t   if ($this->currentTag == "TITLE" || $this->currentTag ==
"LINK" || $this->currentTag == "DESCRIPTION")
/t   {
/t/t   // add data to $channels[] or $items[] array
/t/t   if ($this->flag == 1)
/t/t   {
 
$this->items[$this->count][strtolower($this->currentTag)] .= $data;
/t/t   }
/t/t   else if ($this->flag == 2)
/t/t   {
 
$this->channel[strtolower($this->currentTag)] .= $data;
/t/t   }
/t   }
 }        

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

 // return an associative array of arrays containing item
information
 // (the $items[] array)
 function getItems()
 {
/t   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]))
 {
/t   // print data
/t   $item = $f_items[$x];
/t   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]))
 {
/t   // print data
/t   $item = $s_items[$x];
/t   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]))
 {
/t   // print data
/t   $item = $f_items[$x];
/t   echo "<li><a href=" . $item["link"] . ">" .
$item["title"] . "</a>";
 }
}
?>
</ul>

每次你重新裝入上面的腳本,相應的RDF文件就會被從特定的位置上取來,經過分析之后,按要求的格式顯示出來。

如果你站點具有高的訪問量,你就可能覺得我們的辛苦無意義之極,尤其是當所用的RDF數據更新地沒有那么快時,情況更糟。 在這種情況下,或許探究一下在本地緩存RDF數據才是較明智的做法:要么擴展上面的例子程序,在其中加入緩存功能;要么每閣幾個小時都花很長的時間下載一個最新RDF文件的本地副本到你的Web服務器上,然后使用這個本地副本,而不是那個“活”的(the “live” one)。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲国产精品久久久久久久久久久 | 亚洲精品有限 | 91美女视频在线观看 | 欧美一级毛片美99毛片 | 天堂成人国产精品一区 | 成人三级电影在线 | 欧美日韩国产中文字幕 | 亚洲男人一区 | 久久精品性视频 | 精品国产一区二区三区四区阿崩 | av电影在线免费 | 久久久久999 | 欧美人与牲禽动交精品一区 | 亚洲国产网址 | 一级黄色电影网站 | 色污视频 | 国产一区二区国产 | 天天看成人免费毛片视频 | 在线观看一区二区三区四区 | 亚洲精品欧美二区三区中文字幕 | av电影免费在线看 | 久久久婷婷一区二区三区不卡 | 91成人在线网站 | 中文字幕亚洲一区二区三区 | 韩国一级免费视频 | 毛片免费视频观看 | 黄色伊人网站 | 极品销魂一区二区三区 | 一级免费毛片 | 免费观看又色又爽又黄的崩锅 | av不卡毛片| 国产精彩视频在线 | 毛片小网站| 欧美日韩精品不卡一区二区三区 | 中国a级黄色片 | 多人乱大交xxxxx变态 | 一区在线免费视频 | 一级爱片 | 欧美综合在线观看视频 | 免费看性xxx高清视频自由 | 欧美性受xxxx人人本视频 |