Idiot-Proof Deployment with Phing

disclaimer: I am not underestimating the universe’s ability to produce idiots, the point I’m trying to make is that I haven’t managed to make any deploy mistakes using this approach. Yet.

Once upon a time, a long time ago, I went onto a conference stage for the very first time and said that I thought I might be the world’s ditsiest PHP developer. I actually still think that is pretty true, and if you work with me then you will know that I mostly break and fix things in approximately equal measure. With this in mind, when I launched my own product recently (BiteStats, a thing to automatically email you a summary of your analytics stats every month), I knew that I would need a really robust way of deploying code. I’ve been doing a few different things for a few years, and I’ve often implemented these tools with or for other organisations, but I don’t have much code in production in my own right, weirdly. I decided Phing was the way to go, got it installed, and worked out what to do next.

Continue reading

PHP OAuth Provider: Initial Requirements

I’ve been working with OAuth, as a provider and consumer, and there isn’t a lot of documentation around it for PHP at the moment so I’m sharing my experience in this series of articles. This relates to the stable OAuth 1.0a spec, however OAuth2 has already started to be adopted (and differs greatly). This article uses the pecl_oauth extension and builds on Rasmus’ OAuth Provider post.

OAuth Pages and Endpoints

OAuth has a little more baggage with it than just passing a username and password to an API. As well as your standard service endpoint you will need:
Continue reading

Script for Database Patching at Deploy Time

I’ve written before about a simple way of patching database versions and there’s a much more comprehensive article from Harrie on TechPortal as well. I often find though that projects with patching strategies are missing the scripts to apply these automatically when the code is deployed, so I thought I’d share mine.

My current project (BiteStats, a simple report of your google analytics data) uses a basic system where there are numbered patches, and a patch_history table with a row for every patch that was run, showing the version number and a timestamp. When I deploy the code to production, I have a script that runs automatically to apply the patches.

Continue reading

Invalid Protected Resource URL in Pecl_Oauth

I had a funny (funny weird, not funny haha) problem the other day when working with pecl_oauth in PHP to talk to a service. I’d gone through all the handshaking steps, got the acces token and was ready to start talking to the service itself. However when I tried to call OAuth::fetch, I got an error message:

Fatal error: Uncaught exception 'OAuthException' with message 'Invalid protected resource url, unable to generate signature base string'

There are two things to notice about this. The first one is that I should be catching exceptions thrown by this code :) The second is that I could see nothing wrong with my url, http://api.local. It turned out, after some experimentation, that what is missing here is a trailing slash, and if I supply http://api.local/, everything works perfectly nicely! I’m unclear if this is intended functionality or not, but if you see this error message and you’re requesting a URL with no path info, make sure you have a trailing slash.

Downgrading a PECL Module

Recently I saw some weirdness in an existing application when I upgraded a PECL module that the application depended on. To figure out if that really was the problem, I wanted to downgrade the module to its previous version. There is no opposite command to “upgrade” but you can instruct pecl to install a specific version of a module, using the -f switch to force pecl to overwrite newer modules.

Downgrading Pecl_OAuth

For me the problems were caused in the switch between default functionality in pecl_oauth 1.1.0 (this isn’t a bug, but is correct behaviour according to the OAuth 1 spec, I just had code that expected the old functionality), so I wanted to put my version back to 1.0.0

pecl install -f oauth-1.0.0

It was easy once I stopped looking for an option called “downgrade” or something like that :) In fact you can use this trick to install all kinds of pecl versions, simply refer to the package as [package name]-[version]. By default pecl won’t let you install packages that aren’t marked “stable”, but you can install beta packages by putting “beta” in place of [version].

Hopefully now I’ve written this I’ll remember next time how to do it!

Summer PHP Conferences

Conference season approaches and in May I’m on a trip to take in two of the most high-profile events in the PHP conference calendar: DPC in Amsterdam (19-21 May) and php|tek in Chicago (24-27 May). The two events have historically been a few weeks apart and I’ve always complained at having all the fun for the year in such a short space of time – but this year the events are literally back-to-back, there are a small number of speakers attending both and we’re pretty much all on the same flight from Amsterdam to Chicago!

Continue reading

Github To Jira Bug Migration Script

Recently I mentioned the github API and retrieving issues from it. This is because the joind.in project agreed to move its issue tracking from github to JIRA, since the issue tracker on github is far from feature complete. I migrated only our open issues, and comments (and the comments ended up a bit weirdly formatted on the other end but this was the best they could do). It was nothing pretty or clever but in case it’s useful to someone else, here’s the script:

Continue reading

The PHP Community Conference

Next month, I’ll be heading out to Nashville for the PHP Community Conference. This is wildly exciting for a few reasons; this is the first in what I hope will be many episodes of this event, and I’m speaking in a lineup that blows every other conference schedule I’ve seen out of the water. I have met and hung out with enough of these people to know that I’m going to get smarter just by being there! I love watching the industry leaders discuss technology, I learn so much, and I know that this event will be a fabulous opportunity for that.

The event is entirely community organised and run, rather than being backed by an organisation. I am a great believer in having events come from the community that wants to attend them, and as an organiser (both for community and organisation-backed events) myself, the freedom to do things that will really work, rather than things that can be agreed by a management committee, makes the difference between a good event and a great one. What’s different about this PHP Community Conference is that most of the organisers are speakers and attendees of some of the biggest conferences in the PHP world … and they’ve built the international-level conference *they* want to attend!

The lineup is nothing short of stellar, these guys and gals would be the main feature at most of the other PHP-specific events I’ve been to, in fact three or four of them have been keynotes at other events I’ve attended. I’m speaking myself, which was wildly exciting from the moment I got the acceptance email right up until the rest of the schedule was published … and is now slightly daunting, in the best possible way! I’m giving a half-day tutorial on Web Services, covering all the theory points and showing you how to not only consume but also publish your own services. I work so much with APIs and being able to take the time to properly share my experiences so others can go on to build their own kick-ass services is something really special.

I can’t wait to get out to Nashville on April 21/22 and meet the speakers and the fantastic crowd of attendees that I know an event like this will draw. Which is not to say that there are not other great conferences, but I’m really looking forward to seeing something special in Nashville … I sincerely hope to see you there!

Dealing with MySQL Gone Away in Zend Framework

I wrote recently about having gearman in my application, however I have been seeing problems with the long-running PHP worker scripts. My logs had entries like this:

SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

The worker is a Zend Framework application, run from the CLI, and it seemed like the Zend_Db_Adapter had no way of knowing when MySQL had let go of its end of the connection. I tried a few different things, including Zend_Db_Adapter::getConnection(), but without success – until I dug through the source code (with some help from a friend) and realised that ZF was not reconnecting at all if it thought it already had a connection. So instead, I expressly disconnected and reconnected the database handler. At bootstrap time, I place my database handle into the registry, so I simply added this at the start of the actual function that the gearman worker calls:

$db = Zend_Registry::get('db');
$db->getConnection();

At the end of my script, before it returns to the loop waiting for another gearman job, I just disconnect my database:

$db->closeConnection();

Now Zend_Db_Adapter knows that when I ask it to connect, it needs to go off and make a new connection, and everything works really well! I was seeing the errors because I’m still only testing the system so it can go days between getting any new jobs, and the timeout on MySQL is shorter than that.