Proper JSON encoding with PHP

I’ve decided to write posts that may wind up just being a reference to msyelf. There have been so many times that I wish I could remember how I did things a certain way, and I think that my blog could be a good archive of past programmering. I mean, I’d certainly love it if you did find them useful, but don’t be mad if you don’t.

I recently ran into this nasty bug with our api at work. We started receiving an empty response body along with no errors. Very long story short, it wound up being a json encoding issue. We were using Zend_Json to encode, and my thinking was that there would be some nice wrappers around json_encode to help out with Exceptions, etc. It turns out it just winds up calling json_encode unless you explicitly tell it to use Zend’s built-in encoder.

The problem with json_encode is that it doesn’t throw any sort of exception if it can’t encode what you throw at it. Heck, it doesn’t even return false. What you get back is a nice NULL. That NULL wound up being blindly given back in our api. Oddly enough, errors do happen, and you can get them. Check out json_last_error_msg(). Knowing this, you can handle errors, log them, and maybe even send a friendly message back in the api. In our case, we threw an exception with the attempted string to encode, and the error message that came back from json_last_error_msg.

Check out Handling JSON like a boss in PHP for more info.

Overloading Zend_Soap_Client _doRequest()

I wrote a soap client a while ago using Zend Framework 1 that was consuming data, and needed to log each request for debugging purposes. Thought I’d share a couple of things that I learned.

class My_Test_Soap_Client extends Zend_Soap_Client

public function _doRequest()
$argList = func_get_args();
$options = $this->_getOptions();
if ($options['testsoapclient'] == true) {
$soapLogger = new Zend_Log();
$writer     = new Zend_Log_Writer_Stream($options['logginglocation']);

return call_user_func_array("parent::_doRequest", $argList);

* Don't do this...just here for reference
protected function _getOptions()
return array(
'testsoapclient'  => true,
'logginglocation' => '/some/path/to/log/'

I’m overloading the public function _doRequest() so that I can do the extra processing needed to do my logging. You’ll first notice func_get_args(). I’m using this for my own convenience. I don’t have to stay current with any new arguments that may come.

The other important bit is

call_user_func_array("parent::_doRequest", $argList)

This simply is calling the original parent _doRequest() with the $argList we obtained earlier.

Order by in Zend Framework

Came across a nice “gotcha” using Zend _Db in Zend Framework 1 if you’re being sloppy like I was. When you’re ordering by multiple columns, always make sure to use an array.


$select->order('col1', 'col2', 'col3');


$select->order(array('col1', 'col2', 'col3'));

If you take a look at the order function in Zend_Db_Select, it takes a mixed parameter (string or array) and operates on that. If you’re not seeing PHP warnings, you won’t notice that the parameters ‘col2’ and ‘col3’ are being ignored.

Zend Form presentation with ZF1

Well it’s been nearly a year since I’ve done the presentation, so what better time than now to show it off? I did this presentation on Zend Form using Zend Framework 1 at mkepug.

Download my slides: Zend_Form

Also, awesome new thing I learned! If you own a Mac, and want to present with it. ALWAYS bring your display dongle.