#### 記編寫rss源
點點細雨 ? ? 2013年11月26日星期二
為了提高搜索引擎的收錄速度,今天開始編寫rss源來增加對搜索引擎的友好。
廢話就不多打了,畢竟我打字速度也不快(O(∩_∩)O哈哈~)。
本文為原創,轉載請注明出處!
### 一、開始解析rss文件。
下面是百度國內新聞rss的地址:?[http://news.baidu.com/n?cmd=1&class=civilnews&tn=rss](http://news.baidu.com/n?cmd=1&class=civilnews&tn=rss)
下面是源文件

很顯然這就是一個xml文件?(~?o?~)~zZ
### 二、瀏覽器的解析
那么對于這樣的一個rss文件,瀏覽器是怎樣解析的呢?
ie8:

QQ瀏覽器:

傲游:

谷歌:

火狐:

從上邊看來,源文件為xml的rss源被瀏覽器解析成rss源,就直接可以通過瀏覽器訂閱啦,請無視那個放棄rss功能的谷歌吧!?T_T
### 三、編寫:
分析源代碼,掌握結構

我們要做的就是從我們的網站上獲取到新聞并添加到rss中。
我們參考的方法是phpcms中百度網站地圖的生成方式
下面分析下流程
1.創建xml文件頭
2.寫入新聞
3.加上xml文件結尾
這是創建的步驟,那么我們提交的rss網址的控制器流程呢,當然不能每次打開rss都要進行一次查詢數據庫,所以我們把rss存入文件中,讓控制器直接打開文件,減少服務器壓力。
我的項目是基于thinkphp的,下面是我的控制器,大家一看就明白了
代碼
~~~
<?php
/**
* 網站rss
*
*/
class RssAction extends HomeAction {
public function _initialize() {
parent::_initialize(); //RBAC 驗證接口初始化
$this->header = "<\x3Fxml version=\"1.0\" encoding=\"utf-8\"\x3F>\n<rss version=\"2.0\"><channel>";
$this->footer = "</channel>\n</rss>\n";
$this->rss="";
}
public function rss(){
$config= require './config.php'; //網站配置
$rss_file = $rss_path . 'rss.xml';
if(file_exists($rss_file)){
$rss= file_get_contents($rss_file);
echo $rss;
}else{
$put= $this->index();
$this->rss();
}
}
public function index() {
$config= require './config.php'; //網站配置
//var_dump($config);
$this_domain=$config['web_url'];
//獲取rss配置信息
$rss_path=$config['rss_path'];
$title=$config['rss_title'];
$img_title=$config['rss_img_title'];
$img_link=$config['rss_img_link'];
$img_url=$config['rss_img_url'];
$description=$config['rss_description'];
$link=$config['rss_link'];
$language=$config['rss_language'];
$docs=$config['rss_docs'];
$generator=$config['rss_generator'];
$ttl=$config['rss_ttl']; //文章數量
$NewsDB=D("News");
$newsList=$NewsDB->getAllNews("","news_id desc",$ttl);
//var_dump($newsList);
//循環每條新聞,分別放入數組
$SourceDB = D("Source");
$items=array();
foreach ($newsList as $key => $value) {
$date= date("Y:m-d H:i:s",$value['news_publish_time']);
//獲取來源
$sourceL=$SourceDB->getSource("so_id = ".$value['so_id']);
$source=$sourceL['so_name'];
$url=$this_domain.U('/Home/Index/content',array('id'=>$value['news_id']));
$item= $this->item($value['news_title'],$url,$date,$source,$value['news_author'],$value['news_intro']);
// var_dump($item);
$items[]=$item;
}
//var_dump($this->items);
// var_dump($_SERVER['DOCUMENT_ROOT']);
$rss_file = $_SERVER['DOCUMENT_ROOT'].$rss_path . 'rss.xml';
// echo $rss_file;
@mkdir($dir, 0777, true);
$result= $this->rss_build($items,$rss_file, $title, $img_title, $img_link, $img_url, $description, $link, $language, $docs, $generator, $ttl);
// $this->success("生成站點地圖成功");
//$file=fopen($rss_file,"r") or exit("Unable to open file!");
// var_dump($result);
// echo $result;
}
/**
* rss源的結構
* @param type $title
* @param type $link
* @param type $pubDate
* @param type $source
* @param type $author
* @param type $description
* @return type 數組
*/
private function item($title, $link = '', $pubDate = '', $source = '', $author= '', $description = '') {
$data = array();
$data['title'] = $title;
$data['link'] = $link;
$data['description'] = $description;
$data['author'] = $author;
$data['source'] = $source;
$data['pubDate'] = $pubDate;
return $data;
}
/**
* 生成xml
* @param type $file_name
* @param type $this_domain
* @param type $email
* @param type $time
* @return type
*/
private function rss_build($items,$file_name = null,$title="",$img_title='',$img_link='',$img_url='',$description='',$link='', $language='zh-cn',$docs='', $generator='', $ttl='') {
//百度頭部
$this->rss = '';
$this->rss = $this->header;
$this->rss .= "<title>" . $title . "</title>\n";
$this->rss .= "<image>\n<title>".$img_title."\n</title>\n<link>".$img_link ."\n</link><url>". $img_url ."</url></image>\n";
$this->rss .= "<description>".$description."</description>\n";
$this->rss .= "<link>".$link."</link>\n";
$this->rss .= "<language>" . $language . "</language>\n";
$this->rss .= "<docs>" . $docs . "</docs>\n";
$this->rss .= "<generator>". $generator . "</generator>\n";
$this->rss .= "<ttl>" . $ttl . "</ttl>\n";
foreach ($items as $item) {
$this->rss .= "<item>\n";
$this->rss .= "<title><![CDATA[" . $item['title'] . "]]></title>\n";
$this->rss .= "<link><![CDATA[" . $item['link'] . "]]></link>\n";
$this->rss .= "<pubDate> <![CDATA[" . $item['pubDate'] . "]]></pubDate>\n";
$this->rss .= "<source><![CDATA[" . $item['source'] . "]]></source>\n";
$this->rss .= "<author><![CDATA[" . $item['author'] . "]]></author>\n";
$this->rss .= "<description><![CDATA[" . $item['description'] . "]]></description>\n";
$this->rss .= "</item>\n";
}
$this->rss .= $this->footer . "\n";
if (!is_null($file_name)) {
return file_put_contents($file_name, $this->rss);
} else {
return $this->rss;
}
}
}
~~~
### 四、完善功能
當然,做完上面的還不算完整,為了保證可擴展性,我把rss的標題之類的都做成了配置文件,可以直接通過后臺來修改。
~~~
'rss_path'=>'/tisumoon/',
'rss_title'=>'天樹網新聞站',
'rss_img_title'=>'天樹網',
'rss_img_link'=>'http://www.tisumoon.com/',
'rss_img_url'=>'',
'rss_description'=>'自媒體新聞網站。',
'rss_link'=>'http://www.tisumoon.com/',
'rss_language'=>'zh-cn',
'rss_docs'=>'',
'rss_generator'=>'http://www.tisumoon.com/',
'rss_ttl'=>'10',
~~~
~~~
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>網站RSS設置</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel='stylesheet' type='text/css' href='__PUBLIC__/Admin/css/admin_style.css' />
<script type="text/javascript" src="__PUBLIC__/js/jquery.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/formValidator.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/formValidatorRegex.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.formValidator.initConfig({formid:"myform",autotip:true,onerror:function(msg,obj){
window.top.art.dialog({content:msg,lock:true,width:250,height:100,ok:function(){$(obj).focus();}});
}});
$("#rss_title").formValidator({onshow:"請輸入訂閱標題",onfocus:"請輸入訂閱標題"}).regexValidator({regexp:"ps_username",datatype: "enum",onerror:"應為 中文、字母、數字 或 _ 組成"});
$("#rss_img_title").formValidator({empty:true,onshow:"請輸入圖片提示",onfocus:"請輸入圖片提示"});
$("#rss_img_url").formValidator({empty:true,onshow:"請選擇圖片",onfocus:"請選擇圖片"});
$("#rss_img_link").formValidator({empty:true,onshow:"請輸入圖片鏈接,如:http://www.xxx.com/",onfocus:"請輸入圖片鏈接,如:http://www.xxx.com/"}).regexValidator({regexp:"^http[s]?:\/\/(.+)\/$",onerror:"格式應該為 http://www.xxx.com/,請以‘/’結束"});
$("#rss_link").formValidator({onshow:"請輸入網站鏈接,如:http://www.xxx.com/",onfocus:"請輸入網站鏈接,如:http://www.xxx.com/"}).regexValidator({regexp:"^http[s]?:\/\/(.+)\/$",onerror:"格式應該為 http://www.xxx.com/,請以‘/’結束"});
$("#rss_path").formValidator({onshow:"安裝在根目錄請使用:/",onfocus:"安裝在根目錄請使用:/"}).inputValidator({regexp:"^/\\w*",onerror:"格式應該為 /[xxx]"});
$("#rss_language").formValidator({empty:true,onshow:"請輸入rss語言",onfocus:"默認為‘zh-CN’"}).inputValidator({max:10,onerror:"不能超過10個字符,請確認"});
$("#rss_docs").formValidator({empty:true,onshow:"請輸入docs",onfocus:"請輸入docs"}).inputValidator({max:30,onerror:"不能超過30個字符,請確認"});
$("#rss_description").formValidator({empty:true,onshow:"請輸入網站描述",onfocus:"請輸入網站描述"}).inputValidator({max:50,onerror:"不能超過20個字符,請確認"});
$("#rss_ttl").formValidator({onshow:"rss中展示的新聞條數",onfocus:"rss中展示的新聞條數"}).regexValidator({regexp:"num1",datatype: "enum",onerror:"應為正數"});
}); $("#rss_generator").formValidator({onshow:"請輸入源網站,如:http://www.xxx.com/",onfocus:"請輸入網站鏈接,如:http://www.xxx.com/"}).regexValidator({regexp:"^http[s]?:\/\/(.+)\/$",onerror:"格式應該為 http://www.xxx.com/,請以‘/’結束"});
</script>
</head>
<body>
<form action="?s=Admin/Rss/update" method="post" id="myform">
<table width="98%" border="0" cellpadding="4" cellspacing="1" class="table">
<tr class="table_title"><td colspan="2">RSS訂閱信息設置</td></tr>
<tr class="ji">
<td class="left">訂閱標題</td>
<td><input type="text" name="con[rss_title]" id="rss_title" size="35" maxlength="50" value="{$con.rss_title}">
</td>
</tr>
<tr class="ji">
<td class="left">圖片提示</td>
<td><input type="text" name="con[rss_img_title]" id="rss_img_title" size="35" maxlength="50" value="{$con.rss_img_title}">
</td>
</tr>
<tr class="tr">
<td class="left">圖片鏈接</td>
<td ><input type="text" name="con[rss_img_link]" id="rss_img_link" size="35" maxlength="50" value="{$con.rss_img_link}" >
</td>
</tr>
<tr class="tr">
<td class="left">圖片路徑</td>
<td ><input type="file" name="img_url" id="rss_img_url" size="35" maxlength="50" ><img style="height: 100px;width: auto;" src="{$con.rss_img_url}"></img>
</td>
</tr>
<tr class="tr">
<td class="left">網站鏈接</td>
<td>
<input type="text" name="con[rss_link]" id="rss_link" size="35" maxlength="50" value="{$con.rss_link}" >
</td>
</tr>
<tr class="tr">
<td class="left">網站描述</td>
<td> <textarea name="con[rss_description]" id="rss_description" style="width:500px;height:50px">{$con.rss_description}</textarea></td>
</tr>
<tr class="ji">
<td class="left">語言</td>
<td><input type="text" name="con[rss_language]" id="rss_language" size="35" maxlength="50" value="{$con.rss_language}" >
</td>
</tr>
<tr class="tr">
<td class="left">docs</td>
<td><input type="text" name="con[rss_docs]" id="rss_docs" size="35" value="{$con.rss_docs}">
</td>
</tr>
<tr class="ji">
<td class="left">源網站</td>
<td><input type="text" name="con[rss_generator]" id="rss_generator" size="35" maxlength="50" value="{$con.rss_generator}" ></td>
</td>
</tr>
<tr class="tr">
<td class="left">新聞條數</td>
<td>
<input type="text" name="con[rss_ttl]" id="rss_ttl" size="35" maxlength="50" value="{$con.rss_ttl}">
</td>
</tr>
<tr class="tr">
<td class="left">Rss文件存放路徑</td>
<td>
<input type="text" name="con[rss_path]" id="rss_path" size="35" maxlength="50" value="{$con.rss_path}">
</td>
</tr>
<tr class="ji">
<td colspan="2">
<input class="bginput" type="submit" name="dosubmit" value="提交">
<input class="bginput" type="reset" name="Input" value="重置" >
</td>
</tr>
</table>
</form>
<include file="Index:footer" />
</body>
</html>
~~~
### 五、后記
本來想在網上找些資料把功能做出來,但是國內技術網站相互抄襲,一個文章內容只要標題一樣在所有的網站上都是一樣的,這種現象很不好,嚴重阻礙的技術型網站的進步,所以呢,還是自己研究寫出來吧,本著開源的精神跟大家分享,如果那里不好請一定要指出來啊!
謝謝。
- 前言
- php編寫RSS源
- PHP編寫rss源(續)
- ubuntu 上給PHP安裝擴展 Msgpack 和 Yar
- PHPCMS廣告模塊詳細分析——廣告的生成
- Yii配合Yar在php5.3.3環境下的錯誤以及解決方案
- 【Yaf】Yaf的環境安裝遇到的問題以及解決方案
- 【PHP擴展】centos給PHP安裝擴展
- 【MYSQL】PHPMYADMIN出現的問題以及解決方案
- 【PHP】阿里云升級PHP到5.5詳解
- 【phpMyAdmin】修改配置文件連接到其他服務器
- 【PHP】PHP5.4.0版本ChangeLog詳解(上)
- 【PHP】編譯安裝 PHP5.6.13遇到問題以及解決方案
- 【翻譯】PHP7——新特性
- 【PHP】數組foreach引發的小問題
- 【CURL】PHP的CURL開發項目最佳實踐
- 【PHP】PHP轉換圖片為ico格式源碼
- 【PHP】PHP圖像裁剪縮略裁切類源代碼及使用方法