为什么不能这样实现一个简单的 xml 格式输出模块呢?

由 Caigangshan 于 周六, 2008-07-12 22:40 提交。

我阅读了Drupal的 node 模块中关于输出 rss.xml 的代码,我想实现一个很简单的 xml格式输出模块,将我数据库中的一些信息按照xml格式输出,我写了一个测试模块,代码如下:


<?php
/**
* Implementation of hook_menu().
*/
function test_menu($may_cache) {
 
$items = array();
  if (
$may_cache) { 
  
$items[] = array(
     
'path' => 'test/feed',
     
'title' => t('测试是否可以生成RSS'),
     
'callback' => 'test_feed',
     
'access' => user_access('access content'),
     
'type' => MENU_CALLBACK); 
  }
  return
$items;
}

/**
* A generic function for generating RSS feeds from some data.
*/
function test_feed() {
 
$title = "Test";
 
$link = "http://www.test.com";
  $
$description = "这是一个测试模块!";   
 
$items = test_item($title, $link, $description);
 
 
$namespaces = array('xmlns:dc="http://purl.org/dc/elements/1.1/"');
 
 
$output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
\n";
  $output .= "<rss version=\"1.5.8\" xml:base=\"". $base_url ."\" ". implode(' ', $namespaces) .">\n";
  $output .= $items;
  $output .= "</rss>\n";

  drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
  print $output;
}

function  test_item($title, $link, $description) {
  $output = "<item>\n";
  $output .= ' <title>'. check_plain($title) ."</title>\n";
  $output .= ' <link>'. check_url($link) ."</link>\n";
  $output .= ' <description>'. check_plain($description) ."</description>\n";
  $output .= "</item>\n";
  return $output;
}

我在浏览器中访问 http://test.drupal.cn/?q=test/feed ,却提示浏览器无法下面 test/feed 来自站点 test.drupal.cn,不能这样实现吗?希望大家能够指点一下。