Category Archives: php
Script for Database Patching at Deploy Time
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.
Invalid Protected Resource URL in Pecl_Oauth
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
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
Getting Dates From Week Numbers in PHP
Using DateTime::setISODate
I found that the DateTime extension has a method setISODate() which accepts the year and week number, and makes a date you can then use as normal.
$week_start = new DateTime();
$week_start->setISODate($year,$week_no);
echo $week_start->format('d-M-Y');
You can also do this the other way around; if you have a date in PHP, there’s the ‘W’ flag for date() which will return you the week number – very handy!
Github To Jira Bug Migration Script
The PHP Community Conference
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
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.