Laceweight Purple Mohair (ready)
Saturday, September 6. 2008
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.

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 :)
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.

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)
Friday, September 5. 2008
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.
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 '<?xml version="1.0"?>';
echo "\n";
echo '<response status="ok">';
echo "\n";
foreach($this as $prop_key => $prop_value) {
if(!is_array($prop_value)) {
echo '<'.$prop_key.'>';
echo $prop_value;
echo '</'.$prop_key.'>';
echo "\n";
} else {
echo '<'.$prop_key.'>';
echo "\n";
foreach($prop_value as $array_key => $array_value) {
echo '<'.$array_key.'>';
echo $array_value;
echo '</'.$array_key.'>';
echo "\n";
}
echo '</'.$prop_key.'>';
echo "\n";
}
}
echo "</response>\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".
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 :)
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 '<?xml version="1.0"?>';
echo "\n";
echo '<response status="ok">';
echo "\n";
foreach($this as $prop_key => $prop_value) {
if(!is_array($prop_value)) {
echo '<'.$prop_key.'>';
echo $prop_value;
echo '</'.$prop_key.'>';
echo "\n";
} else {
echo '<'.$prop_key.'>';
echo "\n";
foreach($prop_value as $array_key => $array_value) {
echo '<'.$array_key.'>';
echo $array_value;
echo '</'.$array_key.'>';
echo "\n";
}
echo '</'.$prop_key.'>';
echo "\n";
}
}
echo "</response>\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)
Wednesday, September 3. 2008
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.
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.
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)
Monday, September 1. 2008
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.
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" => 'joe@example.com');
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.
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.
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" => 'joe@example.com');
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
Sunday, August 31. 2008
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 :)
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 :)
Posted by LornaJane
in craft
at
17:44
| Comments (0)
| Trackbacks (0)
Defined tags for this entry: craft
The Wool Shop, Leeds
Thursday, August 28. 2008
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.
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.
Posted by LornaJane
in craft
at
17:51
| Comments (2)
| Trackbacks (0)
Defined tags for this entry: craft
(Page 1 of 60, totaling 359 entries)
» next page



Comments
Sat, 06.09.2008 20:06
Andrew: As magicmonkey says, I ripped out all the extra bits and bobs to make these examples more readable. I think the point about using JSON as a transport is a very valid one t hough, many languages now have built-in support for working with JSON objects so this would be a valid choice. [...]
Sat, 06.09.2008 17:28
Presumably, there’s no escaping / filtering because it’s con centrating on the ReST side of the service, not on the many other best-practice sides of PHP. XML is still quite wide ly used, and it’s potentially a better choice than JSON when (eg) exposing APIs at the edge of your application (i [...]
Sat, 06.09.2008 17:23
Mark: I’m itching to try ubuntu on it but since the device i s so new I’m seeing people still having teething problems ge tting all the peripherals working – and I need this working for an event I’m going to next week so I’m holding my horses for now. Ubuntu with XFCE and the additional netbook [...]
Sat, 06.09.2008 01:19
please fix the example – escape output!!! – cant say how man y time’s i’ve seen commercial code with bugs like that.. JSON is now more commonly used to do this stuff.. XML is us ed mainly for compatibility with REALLY OLD systems now.. ;)
Fri, 05.09.2008 19:01
Nice cosy :) have you tried to install any of the other linu x os’s on it yet? i believe theres a nice version of ubuntu thats designed for netbooks if i deserve a slap for that then stick on slackware for extra l33tness :)
Fri, 05.09.2008 17:47
Olá, hoje no blog da Lorna Jane saiu a terceira e ultima par te de como fazer um REST Server em PHP. Aqui você pode ver o primeiro, segundo e terceiro artigo. Basicamente ele tem o mesmo comportamento do que um SOAP service. Ótimo guia para quem vem …
Wed, 03.09.2008 23:48
Rory: I didn’t abstract the request object but it would be a nice separation to do so. Michael: I haven’t read that b ut I will, thanks for the link. Calling formulaic method na mes seems to a feature of most implementations I’ve seen so far. I haven’t looked at the Zend Framework stuff – t [...]
Wed, 03.09.2008 23:42
Michael: Hey there! Luckily I still have the usual selectio n of other devices at my disposal and this is just for stayi ng in touch when I’m out on the road. I haven’t had any maj or battles with it yet and it certainly weighs a lot less th an my work laptop!
Wed, 03.09.2008 21:26
I’ve recently acquired an EeePC 900 as a quick-fix to a term inally dead laptop. I’d have bought an Aspire One, but it wa sn’t out when I bought mine and I needed a new computer righ t away. Anyway, I can echo your frustrations with hardware i ssues and the default install of Linux not being all [...]