Skip to content

Accessing Twitter via Zend_Service_Twitter

Dear Reader,

Ok, I know there are a lot of OAuth haters out there and maybe it is not the easiest protocol to work with but really, I was able to get connected and running in about 15 minutes. (and that included 5 minutes of Google time)

The Project

I am playing around with Klout’s API and wanted to pull in some info from twitter to augment the Klout info I was seeing. Klout is easy – as just about every Mashery API I’ve worked with has been – you just register for a key and they make your calls. Twitter however, requires OAuth. I knew the Zend_Service_Twitter had recently undergone a revamp to support OAuth but there was one small problem. The script I am playing with is all backend. There is no front end at all. It just collects info and stores it in a database so I can query it later. (I’m an old-school database guy and love just writing ad-hoc queries to see what I can see) Twitter wants to redirect you to a site once you have authorized access. Since I don’t actually have a site to redirect it to, this was a problem.

The Solution – in two parts.

Ok, second things first, this script is straight procedural code at the moment. It isn’t even good enough to qualify as prototype code, more proof-of-concept. There’s no way in heck I’m sharing it with anyone so don’t even ask. I know, however that if things progress as I expect them to, the script will end up as a Zend Framework application. For that reason I wanted to use Zend_Service_Twitter. A quick Google showed me that my good friend and Zend Framework community member Michelangelo van Dam had a tutorial already written on this very subject titled Single User Zend_Service_Twitter. Part 2 is solved because everything I need to know is in that tutorial. So I move on to the first problem, how to get a valid OAuth token and secret. It turns out, that wasn’t hard at all either, it just required a little imagination.

Jaisen Mathai wrote up a blog post titled Twitter, PHP and OAuth a little while back explaining OAuth. In it he links to a quick piece of sample code that he put together to showcase his Twitter API wrapper. The wrapper itself is good and if I had not already decided to go with Zend Framework, I would have used it. The sample code however, allowed me to authenticate with twitter and then store the token and secret, the two things you must have to make this work. So I took his sample code, put it on my development server, followed his instructions to the letter and voilà, I had my token and and token secret.

I then went back to Dragonbe’s blog post, grabbed his code, modified it slightly so that I didn’t have to use Zend_Config and it worked too!


If you want to get started with Twitter’s OAuth, the two tutorials I’ve linked to are great jumping off points. Enjoy!

Until next time,
I <3 |<

Photo Credit:erikeldridge

Breaking into PHP

Dear Reader,

I’ve written a version of this email twice today so I thought I would post it for the other 4 people that read my blog.

The question posed to me is this.

I am new to PHP, but not necessarily to programming. How do I get hired as a PHP developer.

In both cases, I wrote something like this.

Dear XXXX,
First welcome to the PHP Community, there is always room for more! :)

Zend’s new namespace converter

Dear Reader,

Short version

If you have a library of code you want to convert to PHP 5.3 namespaces and it follows the Zend class naming standard, this tool will do the grunt work for you. Clone the repo using git or just grab the archive and start playing with it.

The fun version

RALPH! That’s how these conversations usually start. I can feel Ralph cringe over IM every time I do that too. (And I can see Matthew smiling reading this because it used to be MATTHEW!) Ralph Schindler is one of 2 full-time Zend employees whose job it is to work on the Zend Framework. (Yeah, awesome job if you can get it!) Ralph works on a lost of stuff but my favorite is Zend_Tool. Every time Ralph releases a new version of Zend_Tool, he and I end up having an extended debugging session making sure that my zf twitter client still works.


How I got Zend_Tool working on Windows

Dear Reader,

I love Zend Framework. I love it so much I wrote the very first book about Zend Framework. (Note: It’s no longer the best Zend Framework book, but still, it’s a good one) I’ve written projects using it and I now find myself teaching another class on it. One of the cool things about Zend Framework is it’s cli tool, Zend_Tool. (zf) When zf works, it’s awesome. However, when it breaks, it’s a real pain in the butt. Thanks to buddies like Ralph Schindler though (the author of the tool) when it does break, I can usually get help getting it back up and running.

The latest version of zf that comes with Zend Framework 1.10.x and better is much better than previous version and it shows that Zend (well, at least Ralph) listens when people point out problems and work to solve them.

Even though it’s made great strides, setting up zf is still not seamless. To that end, here is my list of steps needed to setup zf.

My setup

I should note before I start that I am running Windows 7. This means symlinking stuff is right out and we have to resort to physically copying files around. On top of Windows 7, I’m running XAMPP. (anyone know, is that pronounced X-AMP ot ZAMP?) this means that my PHP sits in \xampp\php. I’ve added that directory to my WIndows path so I can execute php from any cmd window. With that understanding, here’s my list.

The steps

  1. Download Zend Framework and unpack it somewhere on your harddrive where you want it to live.
  2. Make sure php.exe is in your PATH and make sure you know where php.exe is.
  3. Find the bin directory in your Zend Framework directory. In it there are 2 files, zf.bat (or for Linux) and zf.php. Copy those to the same directory php.exe is in. (Linux users, feel free to just symlink them)
  4. Your Zend Framework directory should contain a library directory, change directory into it.
  5. from the library directory, test your zf to make sure it works, zf show should give you what you need.
  6. Type zf --setup storage-directory On Windows 7, this will create a directory named .zf in c:\Users\<Your Profile Name>
  7. Type zf --setup config-file. On WIndows 7, this will create a file .zf.ini in c:\Users<Your Profile Name>
  8. Using your favorite text editor, open c:\Users\<Your Profile Name>\.zf.ini. There should only be a single line in it that contains the include_path zf will use. Add the full path to your Zend Framework directory here. It should end in “\library\”.

Test it

That’s all there is to getting it setup. (I remember the days when it took twice as long and required cursing just to get it working) To test it out, cd to any directory other than the Zend Framework directory and type zf show version That should give you the current version of the framework you have installed.

Now you are free to start using it to create projects, etc.

Small problem

There is still a problem with defining your own providers. I tried to add my twitter provider (yes, I can use zf to tweet!) and it won’t yet recognize it. I’ve brought this to Ralph’s attention and impressed upon him the importance of being to tweet from zf. I have no doubt that a solution will be forthcoming.
As pointed out in the comments, this has been fixed, just not documented. This page shows how to add your own providers either manually or via a command.

You can either manually add a line to your .zf.ini

basicloader.classes.0 = "My_ClassName"

or you can use zf to update itself

zf enable config.provider class-name

So now I can tweet from zf again. :)

Until next time,
I <3 |<

[Disclaimer: I use to work at Zend and still have great friends there like the lovely Nili and the walking ray of sunshine that is Andrea. That having been said, it’s been a long time since they gave me any money. So the point of this disclaimer is really just so I can mention Nili and Andrea. :) ]

Update from webcast on Zend_Cache_Frontend_Class

Dear Reader,

In reading over the chat room log for my recent webcast, Zend Framework: Piece by Piece I noticed a question from my buddy Elazar about Zend_Cache_Frontend_Class. Thinking I had made a mistake, I reviewed the materials and the Zend_Cache_Frontend_Class documentation page so that I could do a quick update. The answer wasn’t nearly as simple as I thought.