Laceweight Purple Mohair

I am a habitual chunky-yarn knitter. I will go all the way down to double knit weight, but beyond that I find life is too short to bother :) The upshot of this is that my projects get very big very quickly. I have a few trips coming up where I have long flights, and basically with an 18 hour travel time, I can knit about one hand-lugged-sized quantity of wool!! So I’ve been looking for something more portable to take as my project.

I’ve got this laceweight mohair from http://www.thenaturaldyestudio.com/ and its absolutely gorgeous. The pattern calls for Rohan Kidsilk Haze, which I know is lovely but it really is quite pricey.

laceweight mohair laceweight mohair - in a ball

One of the skeins (I have three!) was wound into a ball by friends when I took it to the knitting group, its 400 yards per skein so I can’t imagine I’m going to manage to crochet all that while I’m away.

The pattern is “Beaded Cobweb Wrap” from Erica Knight’s Essential Crochet, uses a 6mm hook so the pattern is more space than yarn anyway, and it looks quite easy once you’ve done the cast on – its crocheted longways, so there’s a mad long chain that you have to hook into to start with, something I always struggle with. I’ve been assured it’ll look like chewed string until I block it and that I should just carry on regardless – I’ll let you know how I get on :)

PHP Rest Server (part 3 of 3)

Edit: I have a newer series of articles on this topic. You can start reading here :)

This is part 3 of my article about writing a restful service server. If you haven’t already, you might like to read part 1 (covering the core library and grabbing the information we need from the incoming request) and part 2 (covering the service handler itself) before reading this section. This part covers the Response object that I used to return the data to the user in the correct format.

XML Response Object

Exactly what output you want from your service completely depends on the specification and what you are actually trying to do. I wrote a really simple response wrapper that responds with an outline tag containing a status, and then either the content or some error details. Here’s my response class:

class Response {

    public function output() {
        header('Content-type: text/xml');
        echo '';
        echo "\n";
        echo '';
        echo "\n";

        foreach($this as $prop_key => $prop_value) {
            if(!is_array($prop_value)) {
                echo '<'.$prop_key.'>';
                echo $prop_value;
                echo '';
                echo "\n";
            } else {
                echo '<'.$prop_key.'>';
                echo "\n";
                foreach($prop_value as $array_key => $array_value) {
                    echo '<'.$array_key.'>';
                    echo $array_value;
                    echo '';
                    echo "\n";
                }
                echo '';
                echo "\n";
            }
        }
        echo "\n";
    }
}

This could be more elegant and thorough, but its kind of an outline of what I did. Firstly set the content header, then the main tag. Then it loops through all the properties set against the response object and just writes them out as XML. Its very limited but at least its short enough to read easily! There is also an error response that gets called and returns the error type and message, setting the response status to “error” rather than “ok”.

REST Service in PHP

So there you have the ingredients for a REST server in PHP. I’m sure this isn’t the only way to solve this problem, but this is how I did it and I hope it helps someone. If you’ve got any questions, feel free to comment below – and if you do use this for a project, I’d love to hear from you :)

PHP Rest Server (part 2 of 3)

Edit: I have a newer series of articles on this topic. You can start reading here :)

This is part 2 of my rest service writing article. In part 1 we saw the library which holds the functionality we will be using, and we also handled the incoming request and captured all the data we’ll be using.

The REST Service

This is not really tricky but we’re pulling together lots of ideas at this point. So we have already got the incoming request data, and we’ve intialised an object with that data. We have a reference to the object which contains all the functionality we want. And we need to work out which of the library functions we should call, and then transform that data into a good format to return to the user. Let’s start with the handle() function:

    public function handle() {
        $urlParts = parse_url($this->url);
        // substring from 1 to avoid leading slash
        $this->_pathParts = split('/', substr($urlParts['path'], 1));

        // glue the first part of the path to the upper case request method to make a unique function name, e.g. usersGET
        $method = $this->_pathParts[0] . strtoupper($this->method);

        try {
            // now call the method in this class that wraps the one we actually want
            $this->$method();
            $this->response->output();
            return true;
        } catch (Service_Exception_Abstract $e) {
            $this->response->errorOutput($e);
        } catch (Exception $e) {
            die('An Unexpected Error Has Occurred');
        }
        return false;
    }

The rest of this class contains the functions, such as userGET, which call the real worker functions from the library class we originally created. The functions in this section look something like this, and they throw exceptions if anything goes wrong – you can see these being caught in the handle() method posted above:

    protected function userGET() {
        $this->response->user = $this->library->getUserDetails($this->getVars['user_id']);
        return true;
    }

I also use a magic __call() method to return sensible error messages if a user tries to call something that doesn’t exist (this was particularly helpful in development where not everything was there from the start!).

You can see here I’m using setting properties on the response object, but you could also just XML-ify and echo the output here if you wanted to. By using the response object however I am abstracting the XML transformation stuff and moving it to somewhere else so that it can be tested independently and even replaced as needed.

I’ll write about the response class and how that works in the third installment.

PHP Rest Server (part 1 of 3)

I recently had reason to write a REST server in PHP, which was very interesting. There aren’t a whole lot of resources on this topic around so I thought I’d write an outline of what I did. There is quite a lot to it so I’m publishing in multiple sections – this is part 1, which covers the central functionality and handling the incoming request.

Wrapping an Existing Class

The main functionality of the service was written in a class that had no awareness that it was a service – this is a similar approach to the one I described when I wrote about the PHP Soap Server – start with a normal functioning class and write some unit tests for it. The example I’m using today looks like this (my real version actually connects to the database and stuff but this is a nice mock of passing in a number and getting back an array):

class Library {
    public function getUserDetails($user_id) {
        $details = array(
            "user_id" => $user_id,
            "name" => 'Joe Bloggs',
            "email" => '[email protected]');
        return $details;
    }
}

In the soap article, you can see that wrapping a class takes just a few lines – for REST more code is required, but the idea is completely the same. In actual fact we started out with this as a soap service but then it was switched to rest – I only had to re-write the wrapper stuff and not any of the core functionality. Using the wrapper approach would also allow services to be published in multiple ways with the same underlying codebase.

Incoming Requests – Intialising the Wrapper

First of all I wrote a .htaccess file to direct all requests to the one controller file – so that all the different URLs would be handled in the same way.

As the requests came into here, I grabbed the data I needed and initialised the service class, something like this:

$service = new Rest_Service();
// instantiate the main functional class and pass to service
$library = new Library();
$service->setLibrary($library);
// create a response object and pass to service
$response = new Response();
$service->setResponse($response);

// set up some useful variables
$service->url = $_SERVER['REQUEST_URI'];
$service->method = $_SERVER['REQUEST_METHOD'];
$service->getArgs = $_GET;
$service->postArgs = $_POST;
parse_str(file_get_contents('php://input'), $service->putArgs);
parse_str(file_get_contents('php://input'), $service->deleteArgs);

$service->handle();

The variables are all read here and passed in to help decouple the systems – doing it this way allows us to initialise the Service with different incoming variables and/or a different library which can be really useful when testing, as it allows isolation of individual components. The syntax for getting the put and delete data is an interesting one, I wrote about this already in my post on accessing incoming PUT data from PHP, there are some helpful comments on that post as well.

To be continued … later parts will cover the service itself and the response format used.

Crochet Tutorial: Granny Square Round 1

New crochet lesson, and today it really gets interesting! I decided to film the whole row to show you but flickr only allows 90 seconds playback so its in 2 halves :)

Any questions, please feel free to comment here or contact me for help! It does go by quite quickly so do pause and re-watch as needed. And don’t be discouraged if the end result is a bit lop-sided – the aim is to have the foundation ring you started with in the middle, and 4 distinct holes around it. Anything better is a bonus but entirely optional :)

The Wool Shop, Leeds

There’s been a wool shop on Tong Road in Leeds for years, and its always been pretty good. Last year I heard it was closing, the lady who ran it was retiring. To cut a very long story short, it hasn’t closed!!

I’ve been in to inspect and its had a real clean-up, new paint, carpet, better lighting – but its got the same good choice of wools and vast quantities of things being produced from the back room :) Its the same “go and ask at the counter” format but the new owners are nice and enthusiastic and quickly becoming experienced knitters themselves. I dropped in for something and was there for easily 20 minutes! The details are:

The Wool Shop
Whingate Junction
Tong Road
Leeds
LS12 4NQ

Tel: 0113 263 8383

Its on the main bus route, and is easy to park.

Opening hours:
10:30am to 5pm, Monday to Saturday – but they run another mail order business from the premises and are often there much later on, so give them a ring if you want to pop in after 5pm.

Acer Aspire One (and cosy)

I can’t remember another post which was in both the “tech” and “craft” categories – so no complaints from either camp please! Last week I became the new owner of an acer aspire one netbook/ultra mobile PC. Its little, blue, and very cute! If you want to skip the tech bit and read about the cosy I made, click here.

aspire startup screen

The hardware isn’t the best in its class, but it isn’t the most expensive machine either. It has 512MB RAM, 8GB hard drive (solid state drive), it measures 8.9 inches and weighs just 971g.

acer aspire on scales

The default OS for the linux version is something called “Linpus Lite”, which is a kind of toddler fedora as far as I can tell. I’m a long-term linux user and I found the locked-down-ness and the limited interface quite difficult to get started with. However with some help from this nice walkthrough plus some tips from the aspireoneuser.com forums I managed to get root access, turn on the xfce right-click menu and add a menu to the panel, install Opera and Skype and customise the main menu screens. Oh and have multiple desktops, which I like.

Its great for reading feeds, email, chat, and so on, and that’s probably all I’ll use it for. Since I often develop on a dev machine over vim + ssh, I might develop from this machine but I don’t think I’ll develop on it very often. I have already been tripped up by not having particular programs and not knowing what to use instead. Konqueror is leaving a big gap in my life, I do use it a lot. I haven’t done much with fedora before, I’m sure yum is great but it isn’t aptitude. Also the weird “the default user logs in automatically, needs no password, and has instant sudo rights” setup makes me twitch. We tried turning off the sudo rights but bits of the desktop stopped showing up so I’ve left that as is.

There are some definite disappointments. The multi card reader claims to read XD cards – this is a big selling point for me as I have reason to be on the road with camera and laptop soon and I have a fujipix camera which takes an XD card. Well, XD cards do not work with the acer aspire ones so far as I can tell. We’ve dug through the drivers and it looks like it just isn’t set up to do it at all. I’m logged a support email with Acer but no response just yet. I bought the machine through PC World which seems to reduce the amount of support Acer gives, which is a bit disappointing as I didn’t know that before I did it (nobody else seemed to have supply, and they are local if entirely unfriendly. I know I don’t look like a proper geeky business user, but that doesn’t mean you can ignore or patronise me). The wireless won’t resume if it was turned on and the machine hibernates, you have to turn off the wireless before you do that, which seems like a little niggle but when you have lots of tabs active and you have to reboot to get your connection back … its really annoying.

All in all, I would usually have waited for a second generation machine but this is cute, it seems pretty robust, and withouth being very expensive it does everything I need. And at less than 1 kg, I can actually carry it without getting shorter in the process, which is more than I can say for my laptop! There is online evidence of people successfully getting proper ubuntu installations onto these machines and I’m very tempted by that idea.

I have been busy installing the new toy, but I have also been busy making it a cosy!
finished product, outside finished product, inside

Its canvas on the outside, microfibre cloth on the inside (its really shiny and gets fingerprint-y, its good to have the soft inside) and it has rigid panels (made from a plastic placemat that got too close to me while I was feeling inspired). I made two simple envelope-type bags, put them inside one another, and stitched the lining into the outer. The fastenings are sticky dots of velcro (which I shoudl have sewn in because it doesn’t stick well to fabric, oops), and the beads are ones I’ve had in my stash for … more than ten years, scarily enough. They were just waiting for this project! I made the case to be a loose fit, knowing that I’ll try to get A5 paper in there as well as the machine itself as its sort of a convenient size!

Crochet Tutorial: Foundation Ring

Following on from the last lesson, where we did crochet chain with yarn and a hook, this week we use the chain to create the basis for our first project.

It’ll start to look like something after the next one, promise :)

Getting Started with irssi

Irssi is a fabulous IRC client, which runs on the command line. I’m sure I don’t use half the features it offers but its very stable, unintrusive ( you can run it in a background terminal, or even leave it running in a detached screen process ), and frankly excellent. Because I don’t have to set up irssi very often, I always have to look up how to do it – here’s the quick start guide (and remember you can use any of the commands with a “help” argument to get instructions)

First of all, we create a network

/network add -nick  -user  

/network add -nick fred -user fred freenode

Next up, we add a server to the network – this step is so we can add a number of alternative servers for the same network, I never do though.

/server add -noauto -network  

/server add -noauto -network freenode irc.freenode.net

You can also use -auto rather than -noauto above to reconnect to this network every time you use irssi.

Finally, we connect.

/connect 

/connect freenode

PHPNW Site and Call for Papers Launched

Yay! The PHPNW site is now online with all the details of the PHP North West Conference to be held on November 22nd, 2008 in Manchester, UK. The conference is specific to PHP and aims to develop the skills of the developers in the local area. Look out for local speakers, some drinking, and generally a good crowd. Put the date in your diary, tickets will go on sale soon.

We’re also launching the call for papers for this event – it runs til 21st September and I’m really hoping we’ll see some good entries covering a wide range of topics. We are including some shorter slots as well as the traditional 1-hour presentations, so hopefully you can think of something you’d like to talk about for one of those units of time.

If you’ve got any comments or questions about the event, the talks, submitting a paper or anything else PHP-north-west-y, then add a comment or drop me a line.