在我的本地,一切正常,我生成了一个xml文件。 当我将我的代码放在我的网络服务器上时,它会生成一个简单的text / html文件,其中的xml内容在html源代码中可见,但该页面不是作为xml输出生成的。
我尝试过几种我在这里找到的东西,但没有任何效果。
有什么想法吗? (抱歉,我无法正确格式化,链接在这里:http://pastebin.com/D20JZ5cP)
App::uses('AppController', 'Controller');
class RssController extends AppController {
public $components = array('RequestHandler');
public function beforeFilter() {
$this->autoRender = false;
parent::beforeFilter();
}
public function index() {
$this->response->type('text/xml');
// $this->RequestHandler->respondAs('xml');
// $this->RequestHandler->renderAs($this, 'xml');
// $this->RequestHandler->setContent('xml');
// $this->RequestHandler->respondAs('xml');
// $this->RequestHandler->renderAs($this, 'xml');
$this->channel_properties = array(
'title' => 'None',
'link' => 'http://www.example.com',
'description' => 'None',
);
if ( $this->request->query('feed_id') &&
$this->request->query('user_id') ) {
$this->loadModel('Feeds');
$res = $this->Feeds->findAllByFeedIdAndUserAttribution(
$this->request->query('feed_id'),
$this->request->query('user_id'));
if ($res) {
foreach($res AS $entry) {
preg_match_all('/<a.*?>(.*?)<\/a>/', $entry['Feeds']['feed'], $matches);
if (isset($matches[0])) {
foreach($matches[0] AS $key => $link) {
if (stripos($link, 'vine.co') !== false) {
unset($matches[0][$key]);
}
}
$matches[0][] = '[]';
$entry['Feeds']['feed'] = str_replace($matches[0], '', $entry['Feeds']['feed']);
}
$feed[] = array(
'title' => 'No Name',
'link' => 'http://www.example.com',
'description' => $entry['Feeds']['feed'],
'pubDate' => $entry['Feeds']['date_dt'],
// 'category' => $entry['Feeds']['date_dt']
);
}
$this->_createFeed($feed);
}
}
}
private function _createFeed($feed) {
$xml = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
$xml .= '<rss version="2.0"' . $this->xmlns . '>' . "\n";
// channel required properties
$xml .= '<channel>' . "\n";
$xml .= '<title>' . $this->channel_properties["title"] . '</title>' . "\n";
$xml .= '<link>' . $this->channel_properties["link"] . '</link>' . "\n";
$xml .= '<description>' . $this->channel_properties["description"] . '</description>' . "\n";
// channel optional properties
if (array_key_exists("language", $this->channel_properties)) {
$xml .= '<language>' . $this->channel_properties["language"] . '</language>' . "\n";
}
if (array_key_exists("image_title", $this->channel_properties)) {
$xml .= '<image>' . "\n";
$xml .= '<title>' . $this->channel_properties["image_title"] . '</title>' . "\n";
$xml .= '<link>' . $this->channel_properties["image_link"] . '</link>' . "\n";
$xml .= '<url>' . $this->channel_properties["image_url"] . '</url>' . "\n";
$xml .= '</image>' . "\n";
}
// get RSS channel items
// $now = date("YmdHis"); // get current time
// configure appropriately to your environment
foreach ($feed as $rss_item) {
$xml .= '<item>' . "\n";
$xml .= '<title>' . $rss_item['title'] . '</title>' . "\n";
$xml .= '<link>' . $rss_item['link'] . '</link>' . "\n";
$xml .= '<description><![CDATA[' . ($rss_item['description']) . ']]></description>' . "\n";
$xml .= '<pubDate>' . $rss_item['pubDate'] . '</pubDate>' . "\n";
// $xml .= '<category>' . $rss_item['category'] . '</category>' . "\n";
// $xml .= '<source>' . $rss_item['source'] . '</source>' . "\n";
if ($this->full_feed) {
$xml .= '<content:encoded>' . $rss_item['content'] . '</content:encoded>' . "\n";
}
$xml .= '</item>' . "\n";
}
$xml .= '</channel>';
$xml .= '</rss>';
echo $xml;
}
}
答案 0 :(得分:0)
最后它适用于......两次尝试......
App::uses('AppController', 'Controller');
App::uses('Xml', 'Lib');
class RssController extends AppController {
public $components = array('RequestHandler');
public function beforeFilter() {
parent::beforeFilter();
}
public function index() {
$this->RequestHandler->respondAs('xml');
$this->channel_properties = array(
'title' => 'None',
'link' => 'http://www.example.com',
'description' => 'None',
);
if ($this->request->query('feed_id') && $this->request->query('user_id')) {
$this->loadModel('Feeds');
$res = $this->Feeds->findAllByFeedIdAndUserAttribution($this->request->query('feed_id'), $this->request->query('user_id'));
if ($res) {
foreach($res AS $entry) {
preg_match_all('/<a.*?>(.*?)<\/a>/', $entry['Feeds']['feed'], $matches);
if (isset($matches[0])) {
foreach($matches[0] AS $key => $link) {
if (stripos($link, 'vine.co') !== false) {
unset($matches[0][$key]);
}
}
$matches[0][] = '[]';
$entry['Feeds']['feed'] = str_replace($matches[0], '', $entry['Feeds']['feed']);
}
$feeds[] = array(
'title' => 'No Name',
'link' => 'http://www.example.com',
'description' => $entry['Feeds']['feed'],
'pubDate' => $entry['Feeds']['date_dt'],
// 'category' => $entry['Feeds']['date_dt']
);
}
$res = $this->_createFeed($feeds);
$this->set('feeds', $res);
}
}
}
private function _createFeed($feed) {
$xml = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
$xml .= '<rss version="2.0"' . $this->xmlns . '>' . "\n";
// channel required properties
$xml .= '<channel>' . "\n";
$xml .= '<title>' . $this->channel_properties["title"] . '</title>' . "\n";
$xml .= '<link>' . $this->channel_properties["link"] . '</link>' . "\n";
$xml .= '<description>' . $this->channel_properties["description"] . '</description>' . "\n";
// channel optional properties
if (array_key_exists("language", $this->channel_properties)) {
$xml .= '<language>' . $this->channel_properties["language"] . '</language>' . "\n";
}
if (array_key_exists("image_title", $this->channel_properties)) {
$xml .= '<image>' . "\n";
$xml .= '<title>' . $this->channel_properties["image_title"] . '</title>' . "\n";
$xml .= '<link>' . $this->channel_properties["image_link"] . '</link>' . "\n";
$xml .= '<url>' . $this->channel_properties["image_url"] . '</url>' . "\n";
$xml .= '</image>' . "\n";
}
// get RSS channel items
// $now = date("YmdHis"); // get current time // configure appropriately to your environment
foreach ($feed as $rss_item) {
$xml .= '<item>' . "\n";
$xml .= '<title>' . $rss_item['title'] . '</title>' . "\n";
$xml .= '<link>' . $rss_item['link'] . '</link>' . "\n";
$xml .= '<description><![CDATA[' . ($rss_item['description']) . ']]></description>' . "\n";
$xml .= '<pubDate>' . $rss_item['pubDate'] . '</pubDate>' . "\n";
// $xml .= '<category>' . $rss_item['category'] . '</category>' . "\n";
// $xml .= '<source>' . $rss_item['source'] . '</source>' . "\n";
if ($this->full_feed) {
$xml .= '<content:encoded>' . $rss_item['content'] . '</content:encoded>' . "\n";
}
$xml .= '</item>' . "\n";
}
$xml .= '</channel>';
$xml .= '</rss>';
return $xml;
}
}