0

I cannot for the life of me work out why

$xml['interaction']['twitteraccount'] = 'hello';

Causes my JSON output to render as HTML rather than JSON. I've tried all options and played around for a while. Surely I'm missing something? As soon as I take that line out again, it renders as JSON!

public function lifestream()
{
    $this->RequestHandler->setContent('json', 'application/json' ); 

    $this->set('interactions', $this->Interaction->find('all'));

    $xmlArray = array();

    foreach($this->Interaction->find('all') as $interaction) {
        $sourceexploded = explode("/",$interaction['Interaction']['source']);

        if($sourceexploded[0] == "twitter") {
            $xml['interaction']['source'] = $sourceexploded[0];
            $xml['interaction']['twitteraccount'] = 'hello';
        } else {
            $xml['interaction']['source'] = $interaction['Interaction']['source'];
        }

        $xml['interaction']['timestamp'] = $interaction['Interaction']['timestamp'];
        $xml['interaction']['receivedfrom'] = $interaction['Interaction']['receivedfrom'];
        $xmlArray[] = $xml;
    }

    echo json_encode($xmlArray);
3
  • See book.cakephp.org/2.0/en/views/json-and-xml-views.html Commented Aug 5, 2014 at 9:59
  • I've already used and looked at the manual numerous times... Is there something specifically I should be looking at rather than 'the XML and JSON' area of the manual... Commented Aug 5, 2014 at 10:25
  • Well, start reading the link I gave you, your code clearly doesn't look like what is described on that page so I doubt you read it properly. See Thibaults answer as well. Commented Aug 5, 2014 at 12:17

2 Answers 2

3

You have to use the JsonView. In your route.php write: Router::parseExtensions('json');

In your controller you have to set the RequestHandler Component.

class SomeNameController{

 public $components = array('RequestHandler');

 public function lifestream(){
   $this->RequestHandler->setContent('json', 'application/json' ); 
   $this->set('interactions', $this->Interaction->find('all'));
   $xmlArray = array();
   foreach($this->Interaction->find('all') as $interaction) {
    /* do stuff */
    $xmlArray[] = $xml;
   }
   $this->set('data', $xmlArray);
   $this->set('_serialize', array(
            'data', 
    ));
 }

}

Try to go on "samename/lifestream.json" now or make an HTTP request with "Content-Type: application/json".

Look at : http://book.cakephp.org/2.0/en/views/json-and-xml-views.html

Sign up to request clarification or add additional context in comments.

Comments

0

add this 2 line of code:

$this->layout = 'ajax';
$this->autoRender = false;

The first line instructs the render to use an empty layout called ajax (you can find it on Views/Layouts/ajax.ctp

The second one instruct the render to not look for an view template (ctp file)

Then when you echo the json_encode it will rendered as xml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.