php 生成rss文件

司徒良哲
2023-12-01

第一种传统的是直接用php输出语句,按照rss结构规范,新建一个rss文件

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
connect_sql();
$arr = getall();

$files_url = "mytest/test.xml";
/*$xml_head = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";*/
$xml_head = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$xml_head .= "<rss version=\"2.0\">\n";
$xml_head .= "<channel>\n";
$xml_head .= "<title>测试rss</title>\n";
$xml_head .= "<link>http://www.baidu.com</link>\n";
$xml_head .= "<description>这是一个测试rss</description>\n";
writes($xml_head,$files_url);
$xml_body = "";
foreach ($arr as $key => $val) {
$xml_body .= "<item>\n";
$xml_body .= "<user>\n";
$xml_body .= "<user_id>".$val['user_id']."</user_id>\n";
$xml_body .= "<user_name>".$val['username']."</user_name>\n";
$xml_body .= "<user_pass>".$val['pass']."</user_pass>\n";
$xml_body .= "<real_name>".$val['real_name']."</real_name>\n";
$xml_body .= "</user>\n";
$xml_body .= "<title>".$val['username']."</title>\n";
$xml_body .= "<link>http://www.baidu.com</link>\n";
$xml_body .= "<description>id:".$val['user_id'].",username:".$val['username'].",pass:".$val['pass'].",real_name:".$val['real_name']."</description>\n";
$xml_body .= "</item>\n";
}
writes($xml_body,$files_url);


$xml_foot = "</channel>\n";
$xml_foot .= "</rss>\n";
writes($xml_foot,$files_url);


function writes($contents,$url){
$fopen = fopen($url, 'a+');
if(!fwrite($fopen, $contents)){
echo "写入失败!";
}
fclose($fopen);
}


function connect_sql(){
$con = mysql_connect('localhost','root','123456') or die('mysql error :'.mysql_error());
mysql_select_db('test');
mysql_query('set names utf8');
}
function getall(){
$sql = "SELECT * FROM user";
$query = mysql_query($sql);
$content = array();
while($row = mysql_fetch_array($query)){
$content[] = $row;
}
return $content;
}
?>

第二种是利用php DOMdocument组件

$doc = new DOMDocument('1.0','utf-8');
$doc->formatOutput = true;


//创建标签
//创建rss标签
$rss = $doc->createElement('rss');
//创建channel下面的标签
$channel = $doc->createElement('channel');
$ctitle = $doc->createElement('title');
$clink = $doc->createElement('link');
$cdescription = $doc->createElement('description');






foreach ($arr as $key => $val) {
//创建item标签
$item = $doc->createElement('item');
//创建item下的子标签标签
$user = $doc->createElement('user');
$ititle = $doc->createElement('title');
$ilink = $doc->createElement('link');
$idescription = $doc->createElement('description');
//创建user标签
$user_id = $doc->createElement('user_id');
$user_name = $doc->createElement('user_name');
$user_pass = $doc->createElement('user_pass');
$real_name = $doc->createElement('real_name');
/*这里是需要数据库循环调用的地方*/
//创建内容
//创建item下面的标签的内容
$c_ititle = $doc->createTextNode($val['user_id']);
$c_ilink = $doc->createTextNode('http://www.xxx.com');
$c_idescription = $doc->createTextNode('id:'.$val['user_id'].',user_name:'.$val['user_name'].',pass:'.$val['pass'].',real_name:'.$val['real_name']);
//创建user下面的标签的内容
$c_user_id = $doc->createTextNode($val['user_id']);
$c_user_name = $doc->createTextNode($val['user_name']);
$c_user_pass = $doc->createTextNode($val['pass']);
$c_real_name = $doc->createTextNode($val['real_name']);
//创建item属性值
$a_id = $doc->createTextNode($val['user_id']);


//创建属性
$attributes = $doc->createAttribute('id');
/*这里是需要数据库循环调用的地方*/
//item一级标签的元素内容继承
$ititle->appendChild($c_ititle);
$ilink->appendChild($c_ilink);
$idescription->appendChild($c_idescription);
//user一级标签的元素内容继承
$user_id->appendChild($c_user_id);
$user_name->appendChild($c_user_name);
$user_pass->appendChild($c_user_pass);
$real_name->appendChild($c_real_name);
/*这里是需要数据库循环调用的地方*/
//继承
$channel->appendChild($item);
//item一级标签的标签继承
$item->appendChild($user);
$item->appendChild($ititle);
$item->appendChild($ilink);
$item->appendChild($idescription);
//id=1
$attributes->appendChild($a_id);
//<item id="1">
$item->appendChild($attributes);


//item一级标签的标签继承
$user->appendChild($user_id);
$user->appendChild($user_name);
$user->appendChild($user_pass);
$user->appendChild($real_name);
}




//创建内容
//创建channel下面的标签的内容
$c_ctitle = $doc->createTextNode('测试rss');
$c_clink = $doc->createTextNode('http://www.xxx.com');
$c_cdescription = $doc->createTextNode('这是一个测试rss');
//创建rss版本属性值
$rss_attribute_c = $doc->createTextNode('2.0');


//创建rss版本属性
$rss_attribute = $doc->createAttribute('version');


//继承
//channel一级标签的元素内容继承
$ctitle->appendChild($c_ctitle);
$clink->appendChild($c_clink);
$cdescription->appendChild($c_cdescription);
//version="2.0"
$rss_attribute->appendChild($rss_attribute_c);




//channel一级标签的标签继承
$channel->appendChild($ctitle);
$channel->appendChild($clink);
$channel->appendChild($cdescription);








//创建根节点
$rss->appendChild($channel);
$rss->appendChild($rss_attribute);
$doc->appendChild($rss);


//保存xml文件
$doc->save('hello.xml');


function writes($contents,$url){
$fopen = fopen($url, 'a+');
if(!fwrite($fopen, $contents)){
echo "写入失败!";
}
fclose($fopen);
}


function connect_sql(){
$con = mysql_connect('localhost','root','123456') or die('mysql error :'.mysql_error());
mysql_select_db('test');
mysql_query('set names utf8');
}
function getall(){
$sql = "SELECT * FROM test";
$query = mysql_query($sql);
$content = array();
while($row = mysql_fetch_array($query)){
$content[] = $row;
}
return $content;
}

看见这么多代码,我写完的时候也晕了,但是要是一层一层的写还是很简单的

无非就是 创建标签,创建内容,将内容放入标签中,将子标签放入父标签中

$rss = $doc->createElement('rss');

$doc->appendChild($rss);

我是先一层一层的写,首先写成静态的,然后改成调用数据库,这样就变成动态的了

 这样代码过于冗余,于是我写了一个function

//$node是要创建的元素 key是元素名称 value是内容  parent_node是父节点标签名称
function createnode($nodes,$parent_node,$doc){

foreach ($nodes as $key => $value) {
//不论内容存不存在都要创建标签
$label = $doc->createElement($key);
//如果有内容就创建内容节点并添加到标签下
if($value){
$content = $doc->createTextNode($value);
$label->appendChild($content);
}
//将创建好的标签和内容添加到父标签下
$$parent_node->appendChild($label);
}
}

但是貌似不行

文件有了,接着就是显示给订阅了新闻的会员,这里就要读取rss了,类似于下面

$doc = new DOMDocument();
$doc->load("../test.xml");
$data = $doc->getElementsByTagName("real_name");
$arr = array();
foreach ($data as $val) {
$arr[] = $val->nodeValue;
}
print_r($arr);


 类似资料: