1

We are developing an API with the ZEND Framework. Here is an example response

$this->getResponse()
->setHttpResponseCode(200)
->appendBody($this->_helper->json($client->toArray()));

I imagined $this->_helper->json would return a JSON string but its returning an array (in the dev environment).

I now have the staging server to play with and the very same code is returning JSON strings.

The dev environment is currently a different PHP version (dev is 5.3.1, staging is 5.3.3) is this the cause? Do these different versions encode JSON differently? Any idea why this is happening?

JoshH2
  • 55
  • 6
  • Nope it should return json. is it possible some other method is putting it back into an array. $this->_helper->json($array) returns Json right now, it dosen't wait to complete the action. The version of Php should have no bearing on this problem. Although upgrading to 5.3.8 or 5.3.9 is a really good idea to prevent a request object exploit. – RockyFord Jan 30 '12 at 14:42
  • Thanks for the quick response. It is very strange. There are no other functions that touch the output and convert it to an array. As an amend to my previous comment I should say it is returning an array of objects, as if the JSON has been decoded automatically. It does not do this on the staging server. Could it be caused by a character encoding issues as per http://stackoverflow.com/questions/689185/json-decode-returns-null-php? – JoshH2 Jan 30 '12 at 14:56
  • Quick update, @RockFord was correct (my mistake) in that there was a function that was json_decoding the string automatically. However, json_decode fails on the staging server so it looks like its returned a JSOn string to me when in fact it failed to decode it. I am using the solution at stackoverflow.com/questions/689185/json-decode-returns-null-php but is there another was to ensure the character encoding to avoid this? – JoshH2 Jan 30 '12 at 15:40

1 Answers1

0

In this case it would have also been wise to break your nested function calls into separate calls and var_dump each result to verify the inner contents is working as you expected:

$response = $this->getResponse();
$setResponseReturn = $response->setHttpResponseCode(200);
$clientArray = $client->toArray();
var_dump($response, $setResponseReturn, $clientArray);die;

//->appendBody($this->_helper->json($client->toArray()));
Mike Graf
  • 5,077
  • 4
  • 45
  • 58