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.

Zend PHP 5.5 certification studying

I somewhat recently acquired my Zend PHP 5.5 Certification (YAY!!) , and want to share my experience with studying and the test itself. First off, some thoughts on the test. I’m not the best test taker in the world, so to say I was nervous about this one is an understatement. When I entered the room, what followed was one step away from a strip search. I was asked if I had a phone, or anything else in my pockets to I guess help me cheat on the test. If my memory serves me correct, I proceeded to hand over my keys and phone to the friendly lady administering the test. This did not help out my nerves one bit.

The test itself was administered on one of their laptops. There are 70 questions, and you are given 90 minutes to complete them all. Most questions are multiple choice, and some are free text. The free text questions aren’t as scary as they sound. They typically just ask for a small bit of PHP that you’re asked to write out.

To study for the test, I relied heavily on the ZCE study guide. It’s basically an outline of what to study on php.net. I hit every bullet point, and looked on php.net for everything. During the process, I also had a folder of php scripts I wrote to make sure I understood what a function, concept, etc. is. It’s one thing to read it, but completely different to use it.

After taking my notes and writing PHP, I downloaded Lorna Jane’s flashcards. They were extremely helpful to pound the topics I studied down my brain. More importantly they helped me to understand what I did and did not know. Check out her blog post while you’re there. She has some great tips on studying for the exam. I didn’t purchase the sample question pack, but my guess is it’s worth the money.

My recommendation with the test is to schedule the exam as soon as you feel at least somewhat comfortable with the material. I did that, and it was nice to have a deadline to help pressure me into studying. Before I did that, I kind of procrastinated the studying, because there is always something more exciting to do. Don’t be afraid to fail, because even if you don’t pass the first time, you will without a doubt get to better understand the core of PHP throughout your studying. Good luck!

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']);
$soapLogger->addWriter($writer);
$soapLogger->info($argList[1]);
}

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.