Categories
PHP Zend Framework

Database Profiling with Zend Db Profiler Firebug

I don’t know why I haven’t been using this for ages. But Zend’s DB Profiler is fun. Only to be used in development so you can see what your database is doing and how fast/slow it’s doing it.

Enable it in your application’s config.ini (or .xml)

[development]
db.profiler.enabled = true
db.profiler.class   = "Zend_Db_Profiler_Firebug"

And as long as you’re using the DB Factory to create your connection, everything that happens will be automatically profiled and spewed out via Firebug’s awesome logging.  It shows you time spent, number of queries, the queries themselves and parameters used.  It’s awesome.  Why haven’t I been using this before?

You can also enable it directly on your database object if you don’t want it in your config; for example I have block of code that runs only when debugging is enabled (development environment) and as such I might want to enable the profiler there:

$dbAdapter->setProfiler(
       array('enabled' => true, 'class' => "Zend_Db_Profiler_Firebug"));

Yeah, it’s awesome.

Categories
Coding PHP Zend Framework

The Quickest Way To Tweet via Zend Framework’s Twitter Service

This is a quick and dirty method.  It assumes a couple of things.

  1. You’ve already created your ‘app’ on twitter.
  2. You’ve already gotten your personal OAuth tokens from twitter.

First you need to setup your tokens;

$token = new Zend_Oauth_Token_Access();
$token->setToken("YOUR_PERSONAL_TOKEN")
      ->setTokenSecret("YOUR_PERSONAL_SECRET");

Second, you need to setup the Zend_Service_Twitter using your access token as well as the application tokens provided by twitter.

$options = array(
 'accessToken'    => $token,
 'callbackUrl'    => 'http://' .$_SERVER['HTTP_HOST']. '/',
 'consumerKey'    => "YOUR_APPLICATION_KEY",
 'consumerSecret' => "YOUR_APPLICATION_SECRET");

 $twitter = new Zend_Service_Twitter($options);
 $response = $twitter->status->update($message);

And that’s it.

If you want to do other things like authenticate multiple users, you’ll have to do a few other steps. Twitter was nice enough to make it “easy” for a single user to create their own app and post without any goofy intermediate authentication steps.

Categories
PHP

Typecasting with double-not (!!) vs. (bool)

I was asked a strange question today.  “What’s return !! $row; do?” And I had never really been witness to the syntax of the double-not ‘!!’ because really, to me, it’s a bit strange and contradictory for the coding styles I’m used to.  So I looked it up a bit more to see if it was actually faster than proper type casting with (bool)$row in php.  Nobody had any real results. I ran a quick test and sure enough using !! is faster… for arrays.  Other than that, the time is so close it’s insignificant.

Here’s the quick ‘n dirty results:

Time testing (bool) vs. !! conversions – 1,000,000 interations

converted array with (bool) done in 1.06559991837 seconds.
converted array with !! done in 0.534655094147 seconds.

converted ” with (bool) done in 0.572640895844 seconds.
converted ” with !! done in 0.523654937744 seconds.

converted (int) with (bool) done in 0.49324798584 seconds.
converted (int) with !! done in 0.5086581707 seconds.

converted object with (bool) done in 0.564023017883 seconds.
converted object with !! done in 0.556074857712 seconds.

I had to do one million iterations just so the times would add up to something substantial.  Really, the difference is only faster if you happen to want to get a quick ‘true or false’ from an array.  The rest are so insignificant it’s not worth the confusion of a double-not.  Personally, I’d rather see an actual check of $row to see if it contains something useful before returning true or false just based on it being set.  But that’s just me.

Categories
Coding PHP

Dictionary APIs are Fun.

As part of my Hangman! application on Facebook, I plan on adding word definitions.  Like any good word app, it should tell you what the word means.  I’m not a big fan of sending off users to other sites when I don’t have to, so I looked into some dictionary APIs.

These APIs are hard to come by.  Google has a pretty good one, but it’s not open. It’s not open because their third party data providers won’t allow it to be open.  I still whipped up a little PHP love to use it, but I can’t actively use it on a public site because of their restrictions.  So I had to find something else. All the big obvious players like dictionary.com and m-w.com have APIs that aren’t free or fun.

Then I found Wordnik.  They have a sweet, sweet, young API that they were kind enough to give me access to.  As soon as I whip up my own little library to interface with it in a happy efficient way, I’ll be adding definitions and other fun word-related things to Hangman!

Categories
Coding PHP

ZF Tip: Don’t use ‘index’ as a module name.

I’ve been using Zend Framework for a while and when I do my own sites things always work as expected.  But a couple of times in the past I’ve had the honor of working on other sites with other people that have lead me to a few ‘wtf?’ issues.

The most recent one was as to why the shorter urls ie: “/index/pants” would not work.  It would always complain and blow up that the controller ‘pants’ doesn’t exist.  Even though I know for a fact that I want the index controller in the default module and the action ‘pants’…

Categories
Coding PHP Zend Framework

All I Need is REST, Vimeo, Flickr, Zend and More

Here’s my skeleton for my magical REST interface. It’s still missing a lot of method calls for Flickr and Vimeo, but the guts are there and the remaining methods will be done in priority when I need them.  Right now it supports automatic caching and logging via simple options.

At some point I’ll probably put it on a public SVN repository, but for now you can have the tarball.  There’s a README in the file with a simple sample. And the source is pretty self explanatory so you should be able to figure it out if you want to.

The remainder of the work will be done in the order I see fit.  Likely read-only methods (most of the getters) Authentication and write methods will be much much later.

I’ll also add more RESTful interfaces as I need them.  Probably Facebook soon, Twitter later.  Who knows.

Drop your comments here if you’ve got any questions.