Moving Tweet the Tube to AWS Lambda

When I started Tweet the Tube it was a small PHP application triggered by a cron job.

This had been fine for years, until I decided to ditch my hosting provider as it was costing me $20 per month.

I knew that I wanted to move into AWS, but running a nano EC2 instance would still cost me $5 per month. Not a lot of money but still more than I wanted to pay for such a small application.

Then I found out that AWS Lambda had a free tier. Better yet it meant no servers to manage. Seemed like a win-win to me!

Read more »

Flintstone 2.0 released

I am delighted to announce version 2.0 of Flintstone has now been released.

Changes include:

  • Major refactor, class names have changed and the whole codebase is much more extensible
  • Removed the static load and unload methods and the FlinstoneDB class
  • The replace method is no longer public
  • The getFile method has been removed
  • Default swap memory limit has been increased to 2MB
  • Ability to pass any instance for cache that implements Flintstone\Cache\CacheInterface

View on Github

Data transformer class

When building an API it is common for people to just grab stuff from the database and pass it to json_encode(). The problem with this approach is that it can quickly lead to inconsistent output - for example when a database table schema changes.

A data transformer acts as the middle-man between the data fetched and what is output to ensure consistency. Think of it as a view layer for your data. Below is a transformer class and example that you can extend to write your own transformers.

Read more »

The working directory in PHP

A colleague recently came to me with this issue that worked fine in their browser, but not on the command line:

Warning: include(../config.php): failed to open stream: No such file or directory

This warning appeared because of the current working directory.

When accessing this script via the browser, Apache found the correct path to the file and so '../' was relative to it's own directory.

However by running it from a command line...

php /var/www/public/index.php

The current working directory was actually wherever the user is in the file system of the server.

In which case they could do:

cd /var/www/public/
php index.php

Better yet, they could use the __DIR__ magic constant in PHP which returns the directory of the file itself.

include(__DIR__ . '/../config.php');

If you ever need to figure out the current working directory, you can use the nifty function getcwd().

Google Maps - dragging a marker to new position

Something that took me a while to figure out was how to drag a marker on a map to a new position and then retrieve it's lat/lng (version 3).

This assumes you have the map variable set as an instance of google.maps.Map and latlng which is the current position to show on the map as an instance of google.maps.LatLng.

var marker = new google.maps.Marker({
	map: map,
	draggable: true,
	position: latlng

google.maps.event.addListener(marker, 'dragend', function(){
	var position = marker.getPosition();
	map.setCenter(position); // set the map center to it's new position
	alert( + ',' + position.lng()); // here are the new lat/lng strings