Ер нь XML юу чадах талаар Marchal – XML by Example номноос товч үзүүлье:
Some of the areas where XML will be useful in the near-term include:- large Web site maintenance. XML would work behind the scene to simplify the creation of HTML documents
- exchange of information between organizations
- offloading and reloading of databases
- syndicated content, where content is being made available to different Web sites
- electronic commerce applications where different organizations collaborate to serve a customer
- scientific applications with new markup languages for mathematical and chemical formulas
- electronic books with new markup languages to express rights and ownership
- handheld devices and smart phones with new markup languages optimized for these “alternative” devices
Эдгээрээс миний үзүүлэх жишээ бол дээрээсээ 4 дах буюу Syndicated content ашиглан ондоо веб сайтууд нэг мэдээллийг тараах тухай юм. Уг жишээг хийх болсон шалтгаан нь Хөл бөмбөгийн дэлхийн аварга шалгаруулах тэмцээний мэдээллийг мэдээллийн хүчирхэг хоёр суваг болох Yahoo, Google – хоёрын тусламжтайгаар нэгтгэн үзэх гэсэн хүслээс минь үүдсэн юм.
Та хаа нэгтээ сайтан дээр RSS гэсэн XML линк байхыг харсан байх. Энэ нь тэр сайт тухайн контентоо бусадтай XML – технологийг ашиглан хуваалцах сонирхолын үүднээс хийж тавьжээ гэж ойлгож болно.
Миний хувьд Google News- дээр FIFA Germany 2006 гэсэн ангилал үүсгээд тэр ангилалд харгалзах RSS –ыг, мөн адил Yahoo – дээрх FIFA Germany 2006 -ын RSS-тэй харьцуулах байдлаар гаргаж тавьсан юм.
Үүний тулд ямар код ашигласанаа одоо та бүхэнд дэлгэе.
<?php
class lastRSS {
var $default_cp = 'UTF-8';
var $CDATA = 'content';
var $cp = '';
var $items_limit = 0;
var $stripHTML = False;
var $date_format = '';
var $itemtags = array('title', 'link', 'description', 'author', 'category', 'comments', 'enclosure', 'guid', 'pubDate', 'source');
var $imagetags = array('title', 'url', 'link', 'width', 'height');
var $textinputtags = array('title', 'description', 'name', 'link');
if ($this->cache_dir != '') {
$cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url);
$timedif = @(time() - filemtime($cache_file));
if ($timedif < $this->cache_time) {
$result = unserialize(join('', file($cache_file)));
if ($result) $result['cached'] = 1;
} else {
$result = $this->Parse($rss_url);
$serialized = serialize($result);
if ($f = @fopen($cache_file, 'w')) {
fwrite ($f, $serialized, strlen($serialized));
fclose($f);
}
if ($result) $result['cached'] = 0;
}
}
else {
$result = $this->Parse($rss_url);
if ($result) $result['cached'] = 0;
}
return $result;
}
function my_preg_match ($pattern, $subject) {
preg_match($pattern, $subject, $out);
if(isset($out[1])) {
if ($this->CDATA == 'content') {
$out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
} elseif ($this->CDATA == 'strip') {
$out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
}
if ($this->cp != '')
$out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]);
return trim($out[1]);
} else {
return '';
}
}
function unhtmlentities ($string) {
$trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES);
$trans_tbl = array_flip ($trans_tbl);
$trans_tbl += array(''' => "'");
return strtr ($string, $trans_tbl);
}
function Parse ($rss_url) {
if ($f = @fopen($rss_url, 'r')) {
$rss_content = '';
while (!feof($f)) {
$rss_content .= fgets($f, 4096);
}
fclose($f);
$result['encoding'] = $this->my_preg_match("'encoding=[\'\"](.*?)[\'\"]'si", $rss_content);
if ($result['encoding'] != '')
{ $this->rsscp = $result['encoding']; }
else
{ $this->rsscp = $this->default_cp; }
preg_match("'<channel.*?>(.*?)</channel>'si", $rss_content, $out_channel);
foreach($this->channeltags as $channeltag)
{
$temp = $this->my_preg_match("'<$channeltag.*?>(.*?)</$channeltag>'si", $out_channel[1]);
if ($temp != '') $result[$channeltag] = $temp;
}
if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) {
$result['lastBuildDate'] = date($this->date_format, $timestamp);
}
preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si", $rss_content, $out_textinfo);
if (isset($out_textinfo[2])) {
foreach($this->textinputtags as $textinputtag) {
$temp = $this->my_preg_match("'<$textinputtag.*?>(.*?)</$textinputtag>'si", $out_textinfo[2]);
if ($temp != '') $result['textinput_'.$textinputtag] = $temp;
}
}
preg_match("'<image.*?>(.*?)</image>'si", $rss_content, $out_imageinfo);
if (isset($out_imageinfo[1])) {
foreach($this->imagetags as $imagetag) {
$temp = $this->my_preg_match("'<$imagetag.*?>(.*?)</$imagetag>'si", $out_imageinfo[1]);
if ($temp != '') $result['image_'.$imagetag] = $temp;
}
}
preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items);
$rss_items = $items[2];
$i = 0;
$result['items'] = array();
foreach($rss_items as $rss_item) {
if ($i < $this->items_limit || $this->items_limit == 0) {
foreach($this->itemtags as $itemtag) {
$temp = $this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si", $rss_item);
if ($temp != '') $result['items'][$i][$itemtag] = $temp;
}
if ($this->stripHTML && $result['items'][$i]['description'])
$result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description'])));
if ($this->stripHTML && $result['items'][$i]['title'])
$result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title'])));
if ($this->date_format != '' && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !==-1) {
$result['items'][$i]['pubDate'] = date($this->date_format, $timestamp);
}
$i++;
}
}
$result['items_count'] = $i;
return $result;
}
else
{
return False;
}
}
}
?>
Filename: index.php
<?
// include lastRSS library
include './lastRSS.php';
$rss = new lastRSS;
// setup transparent cache$rss->cache_dir = './cache';
$rss->cache_time = 3600; // one hour
if ($rs = $rss->get('http://fifaworldcup.yahoo.com/06/en/fwc.rss?myYahoo=true')) {
echo "<font color='#FFBA00'><b>Last Build Date: </b></font><font color='#FCFF00'>".$rs['lastBuildDate']."</font>";
echo "<ul>\n";
foreach($rs['items'] as $item) {
$publish = strtotime($item['pubDate']);
echo "\t<a href=\"$item[link]\">".$item['title']."</a><br />\n";
echo "\t".$item['description'];
echo date("Y-m-d H:s", $publish);
}
echo "</ul>\n";
}
else {
die ('Error: RSS file not found...');
}
?>
Амжилт хүсье.
No comments:
Post a Comment