Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return only JSON from Zend

Tags:

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.

like image 737
Gia Duong Duc Minh Avatar asked Feb 08 '13 16:02

Gia Duong Duc Minh


1 Answers

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 
like image 185
Venu Avatar answered Sep 28 '22 15:09

Venu