views:

1127

answers:

2

Hi all,

I'm having problems returning XML in my ZF Application. Below is the code I have:

<?php
class ProjectsController extends Gid_Controller_Action
{
 public function xmlAction ()
 {
  $content = "<?xml version='1.0'><foo>bar</foo>";
  header('Content-Type: text/xml');
  echo $content;
 }
}
?>

I've also tried the following:

<?php
class ProjectsController extends Gid_Controller_Action
{
 public function xmlAction ()
 {
  $content = "<?xml version='1.0'><foo>bar</foo>";
  $this->getResponse()->clearHeaders();
  $this->getResponse()->setheader('Content-Type', 'text/xml');
  $this->getResponse()->setBody($content);
  $this->getResponse()->sendResponse();
 }
}
?>

Could someone point me in the right direction as to how I go about achieving this? Thank you very much.

+1  A: 

You're missing the ending question mark on the xml tag:

<?xml version='1.0'>

It should be

<?xml version='1.0'?>

Additionally, you will probably need to disable your layout so it prints only the xml. Put this line in your xmlAction() method

$this->_helper->layout->disableLayout();

You may want to consider the contextSwitch action helper

Also, you may want to use DomDocument instead of typing out xml directly

Mark
Thanks for the help! :)
Jay
+4  A: 
Oleksandr Bernatskyi
Thanks for the help! :)
Jay
You're welcome :)
Oleksandr Bernatskyi