23

I'm using Zend Framework 1.x for my project. I want to create a Web service return only JSON string for the caller function. I tried to use Zend_Controller_Action and applied those ways:

1.

$this->getResponse()
     ->setHeader('Content-type', 'text/plain')
     ->setBody(json_encode($arrResult));

2.

$this->_helper->getHelper('contextSwitch')
              ->addActionContext('nctpaymenthandler', 'json')
              ->initContext();

3.

header('Content-type: application/json');

4.

$this->_response->setHeader('Content-type', 'application/json');

5.

echo Zend_Json::encode($arrResult);
exit;

6.

return json_encode($arrResult);

7.

$this->view->_response = $arrResult;

But when I used cURL to get result, it returned with JSON string surrounded by some HTML tags. Then I tried to user Zend_Rest_Controller with the options above. It still did not success.

P.S.: Most of those ways above are from the question which had been asked on Stack Overflow.

4 Answers 4

47

I Like this way!

//encode your data into JSON and send the response
$this->_helper->json($myArrayofData);
//nothing else will get executed after the line above
Sign up to request clarification or add additional context in comments.

3 Comments

I have used this method for some time. I don't understand the need for all the additional code. The helper method handles everything for you as far as I can tell.
where to put this code ? In controller's action function ?
@HarisMehmood your controller's action is the right place to put it as it is its role to handle the request and prepare the output.
14

You need to disable the layout and view rendering.

Explicit disable layout and view renderer:

public function getJsonResponseAction()
{
    $this->getHelper('Layout')
         ->disableLayout();

    $this->getHelper('ViewRenderer')
         ->setNoRender();

    $this->getResponse()
         ->setHeader('Content-Type', 'application/json');

    // should the content type should be UTF-8?
    // $this->getResponse()
    //      ->setHeader('Content-Type', 'application/json; charset=UTF-8');

    // ECHO JSON HERE

    return;
}

If your using the json controller action helper you need to add a json context to the action. In this case the json helper will disable the layout and view renderer for you.

public function init()
{
    $this->_helper->contextSwitch()
         ->addActionContext('getJsonResponse', array('json'))
         ->initContext();
}

public function getJsonResponseAction() 
{
    $jsonData = ''; // your json response

    return $this->_helper->json->sendJson($jsonData);
}

1 Comment

The Venu's method is better !
9

Your code would need to disable the layout as well in order to stop the content being wrapped with the standard page template. But a much easier approach would just be:

$this->getHelper('json')->sendJson($arrResult);

the JSON helper will encode your variable as JSON, set the appropriate headers and disable the layout and view script for you.

Comments

1

It is much easier.

public function init()
{
    parent::init();
    $this->_helper->contextSwitch()
        ->addActionContext('foo', 'json')
        ->initContext('json');
}

public function fooAction()
{
    $this->view->foo = 'bar';
}

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.