|
先建兩個資料表
資料表名為 webref_rss_details- CREATE TABLE 'webref_rss_details' (
- 'id' int(11) NOT NULL auto_increment,
- 'title' text NOT NULL,
- 'description' mediumtext NOT NULL,
- 'link' text,
- 'language' text,
- 'image_title' text,
- 'image_url' text,
- 'image_link' text,
- 'image_width' text,
- 'image_height' text,
- PRIMARY KEY ('id')
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
複製代碼 及webref_rss_items- CREATE TABLE 'webref_rss_items' (
- 'id' int(11) NOT NULL auto_increment,
- 'title' text NOT NULL,
- 'description' mediumtext NOT NULL,
- 'link' text,
- PRIMARY KEY ('id')
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
複製代碼 資料表 webref_rss_details 包含10個欄位,有id, title, description, link, language, image_title, image_url, image_link, image_width and image_height
第二個資料表 webref_rss_items 包含四個欄位 id, title, description, link
建立有效的PHP RSS 2.0 feed 標頭,並使用RSS()物件- <?
- header("Content-Type: application/xml; charset=ISO-8859-1");
- include("classes/RSS.class.php");
- $rss = new RSS();
- echo $rss->GetFeed();
- ?>
複製代碼 建立一個 mysql_connect.php 檔案連接資料庫,裡面定義著所有連接資料庫的資訊欄位- <?
- DEFINE ('DB_USER', 'your_username');
- DEFINE ('DB_PASSWORD', 'your_password');
- DEFINE ('DB_HOST', 'localhost');
- DEFINE ('DB_NAME', 'your_databasename');
- // Make the connnection and then select the database.
- $dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
- mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
- ?>
複製代碼 要建立RSS之前,必須要先連接資料庫,先連接 mysql_connect.php
在建立 RSS class 裡的 GetFeed 函式
第一個方法先 called getDetails 函式
然後連接 webref_rss_details 資料表,和取出裡面所有的值,用 while 迴圈方法取出
第二個方法在私有函式裡,GetFeed called getItems,這方法會選取 webref_rss_items 資料表裡所有的值,然後產生RSS規範的xml,然後寫入檔案
RSS.class.php檔案程式如下- <?
- class RSS {
- public function RSS() {
- require_once ('pathto.../mysql_connect.php');
- }
- public function GetFeed() {
- return $this->getDetails() . $this->getItems();
- }
- private function dbConnect() {
- DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
- }
- private function getDetails() {
- $detailsTable = "webref_rss_details";
- $this->dbConnect($detailsTable);
- $query = "SELECT * FROM ". $detailsTable;
- $result = mysql_db_query (DB_NAME, $query, LINK);
- while($row = mysql_fetch_array($result)) {
- $details = '<?xml version="1.0" encoding="ISO-8859-1" ?>
- <rss version="2.0">
- <channel>
- <title>'. $row['title'] .'</title>
- <link>'. $row['link'] .'</link>
- <description>'. $row['description'] .'</description>
- <language>'. $row['language'] .'</language>
- <image>
- <title>'. $row['image_title'] .'</title>
- <url>'. $row['image_url'] .'</url>
- <link>'. $row['image_link'] .'</link>
- <width>'. $row['image_width'] .'</width>
- <height>'. $row['image_height'] .'</height>
- </image>';
- }
- return $details;
- }
- private function getItems() {
- $itemsTable = "webref_rss_items";
- $this->dbConnect($itemsTable);
- $query = "SELECT * FROM ". $itemsTable;
- $result = mysql_db_query (DB_NAME, $query, LINK);
- $items = '';
- while($row = mysql_fetch_array($result)) {
- $items .= '<item>
- <title>'. $row["title"] .'</title>
- <link>'. $row["link"] .'</link>
- <description><![CDATA['. $row["description"] .']]></description>
- </item>';
- }
- $items .= '</channel>
- </rss>';
- return $items;
- }
- }
- ?>
複製代碼 參考原文 http://www.webreference.com/authoring/languages/xml/rss/custom_feeds/ |
|