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.