Kristian Lunde

www.klunde.net

Archive for June, 2008

whoisi.com

without comments

John Resig posted a note on his blog about whoisi.com, a social network aggregator. The system is ingeniously simple, do a search for the name you are looking for, I did a search for myself :P .
All similar names are listed, if the name is not listed, just add it. To each name you can attach a bunch of links, as for instance facebook, twitter, linkedIn, blogs and so on. Everyone has the possibility to add a person and attach a links.

My page on http://whoisi.com/p/1383

Written by Kristian Lunde

June 27th, 2008 at 9:40 pm

Posted in Applications

Tagged with ,

Firefox killing the sound on my Ubuntu box

without comments

Tonight I encountered a rather strange problem on my Ubuntu box. While doing nothing special on my machine, just surfing and listening to music and suddenly there are no sound?!?

Doing a quick search on ubuntuforums.org I quickly found the solution. The problem seems to be Firefox, killing the firefox sessions and restarting the alsa-utils did the trick for me, and voila the sound is back :D

The howto get the sound back is found here: http://ubuntuforums.org/showthread.php?p=2742327

BTW: starting firefox again did not cause any problems ;)

Written by Kristian Lunde

June 27th, 2008 at 9:25 pm

Posted in Linux,Ubuntu

Tagged with , ,

PHP Vikinger

without comments

I attended the PHP vikinger unconference in Skien on Saturday. Rather than writing my own wrap up of the unconference I choose to link to the wrap up written by Mats Lindh and Derick Rethans

Notes from Mats: http://e-mats.org/2008/06/php-vikinger-notes/

Notes from Derick: http://phpvikinger.org/news/news-2008-05-23

Written by Kristian Lunde

June 23rd, 2008 at 8:20 pm

Posted in PHP,Real Life

Tagged with ,

Project managment

with one comment

Adaptive path has a brilliant article about project management and how to create a center of creativity and productivity.

URL: http://www.adaptivepath.com/blog/2008/06/11/the-magic-number-one-how-project-managers-foster-creativity/

I’ve had the chance to work with a couple of project managers which had the same philosophy as described in the adaptive path article, and can with certainty say that such an approach does work and give the project members a real boost of creativity and efficency.


Written by Kristian Lunde

June 12th, 2008 at 11:52 am

I’m looking for a job in Newcastle (UK)

without comments

I’ve started to look for a job in Newcastle Upon Tyne since my girlfriend and daugther are moving there in September. I’ve posted my resume and profile on totaljobs.co.uk, monster.co.uk, cwjobs.co.uk and justengineers.net. I’ve also set up a few job mailers which sends me the most recent IT jobs registered on the sites mentioned above.

So far I haven’t found any perfect matches, however, I’ve applied on a few jobs that sounds interesting.

Please take a look at my linkedin profile or resume If you are hiring and looking for a web developer or software engineer, and do not hesitate to contact me.

It is a bit sad that I have to resign from my position in Derdubor AS, because I got a couple of really good colleagues there and I learn a lot working there. However, I’m looking forward to new challenges in Newcastle.

Written by Kristian Lunde

June 10th, 2008 at 10:46 pm

Posted in Real Life

Tagged with , ,

Writing SOA applications with PHP

without comments

Lately I’ve been working a lot with data integration between several web applications, and a natural choice for the integration was to use a Service Oriented Architecture (SOA). I’ve built both SOA servers and clients before using the SOAP approach, which is a superb way of transferring data when not knowing who the user of the service is, or when you are a client of such a service. However in this setting I knew who the user was, I knew what the service would be used for, also had the chance to write both the client and server.

The choice fell on a simple implementation using the JSON (JavaScript Object Notation) data structure. This is really easy using the built in json library in php. The server use the json_encode() function and the implemented sub class of the client use the json_decode() function.

To simplify this further I implemented two abstract classes, a ServiceServer class and a ServiceClient class. The service server class contained a very simple displayJSONResult function, which sets the correct header, encode the data as json data and echo the data.

The service client class has two main functions, doGetRequest and doPostRequest, both functions use the cURL library in PHP.

Server

  1. <?php
  2.  
  3. abstract class ServiceServer
  4. {
  5.  
  6.  public function __construct(){}
  7.  
  8.  protected function displayJSONResult($data)
  9.  {
  10.   header('Content-type: text/plain');
  11.  
  12.   echo json_encode($data);
  13.  
  14.   exit();
  15.  }
  16. }

Client

  1. <?php
  2.  
  3. /**
  4.  * ServiceClient
  5.  *
  6.  *
  7.  * @author Kristian Lunde
  8.  *
  9.  */
  10.  
  11. class ServiceClient
  12. {
  13.  
  14.  public function __construct(){}
  15.  
  16.  /**
  17.   * do a post request to a service
  18.   *
  19.   * the params parameter must be a string with the format:
  20.   * key=val&key2=val2&key3=val3
  21.   *
  22.   * @param string $url
  23.   * @param string $params
  24.   */
  25.  protected function doPostRequest($url, $params)
  26.  {
  27.   $ch = curl_init($url);  
  28.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
  29.   curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
  30.   curl_setopt($ch, CURLOPT_POST   , 1);
  31.    curl_setopt($ch, CURLOPT_POSTFIELDS     , $params);
  32.   $result = curl_exec($ch);
  33.   curl_close($ch);
  34.   return $result;
  35.  }
  36.  
  37.  /**
  38.   * do a get request to a service
  39.   *
  40.   * @param string $url
  41.   *
  42.   * @return mixed
  43.   */
  44.  protected function doGetRequest($url)
  45.  {
  46.   $ch = curl_init($url);  
  47.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
  48.   curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
  49.   $result = curl_exec($ch);
  50.   curl_close($ch);
  51.   return $result;
  52.  }
  53.  
  54. }

Example

To illustrate the easiness and simplicity of this SOA approach I written a small example. In this example the server finds the country of a city using POST parameters or it can find cities using the country as a GET parameter.

Example server

  1. <?php
  2.  
  3. /**
  4.  * Example of using the ServiceServer class
  5.  *
  6.  * Returns countries or cities
  7.  *
  8.  * @author Kristian Lunde
  9.  */
  10.  
  11. require_once('ServiceServer.php');
  12.  
  13. class ExampleServer extends ServiceServer
  14. {
  15.  
  16.  
  17.  
  18.  public function __construct()
  19.  {
  20.   $this->countries = array('norway'  => array('Oslo',
  21.               'Trondheim',
  22.               'Bergen',
  23.               'Halden',
  24.               'Sarpsborg',
  25.               'Hammerfest'),
  26.          'sweden' => array('Stockholm',
  27.               'Gothenburg',
  28.               'Karlstad'),
  29.          'england' => array('London',
  30.               'Newcastle',
  31.               'Bath',
  32.               'Liverpool'));  
  33.  
  34.  
  35.   if(isset($_GET['country']))
  36.   {
  37.    $this->findCitiesByCountry(trim($_GET['country']));
  38.   }
  39.  
  40.   if(isset($_POST['city']))
  41.   {
  42.    $this->findCountryByCity(trim($_GET['city']));
  43.   }
  44.  }
  45.  
  46.  public function findCitiesByCountry($country)
  47.  {
  48.   $cities = 'Not found';
  49.   $country = strtolower($country);
  50.  
  51.   if(isset($this->countries[$country]))
  52.   {
  53.    $cities = $this->countries[$country];
  54.   }
  55.  
  56.   $this->displayJSONResult($cities);
  57.  }
  58.  
  59.  public function findCountryByCity($city)
  60.  {
  61.   $country = 'Not found';
  62.  
  63.   $break = false;
  64.   foreach($this->countries as $key => $val)
  65.   {
  66.    for($i = 0, $count = count($val); $i < $count; $i++)
  67.    {
  68.     if($city == $val[$i])
  69.     {
  70.      $country = $key;
  71.      $break = true;
  72.      break;
  73.     }
  74.    }
  75.    
  76.    if($break)
  77.    {
  78.     break;
  79.    }
  80.   }
  81.  
  82.   $this->displayJSONResult($country);
  83.  }
  84. }
  85.  
  86. $obj = new ExampleServer();
  87. ?>

Example client

  1. <?php
  2. /**
  3.  * Example of using the ServiceClient class
  4.  *
  5.  * does a request to the ExampleServer
  6.  *
  7.  * @author Kristian Lunde
  8.  */
  9.  
  10. require_once('ServiceClient.php');
  11.  
  12. class ExampleClient extends ServiceClient
  13. {
  14.  public function __construct()
  15.  {
  16.   $this->getCities('Norway');
  17.   $this->getCounty('Bath');
  18.  }
  19.  
  20.  public function getCities($country)
  21.  {
  22.   $url = 'http://files.klunde.net/files.klunde.net/ExampleServer.php?country=' . $country;
  23.   echo $this->doGetRequest($url);
  24.  }
  25.  
  26.  public function getCountry($city)
  27.  {
  28.   $url = 'http://files.klunde.net/files.klunde.net/ExampleServer.php';
  29.   $params = 'city=' . $city;
  30.  
  31.   echo $this->doPostRequest($url, $params);
  32.  }
  33. }
  34. $obj = new ExampleClient();
  35. ?>

I’m quite satisfied with this implementation because it is so easy to maintain and even more important, implementing new services and clients for the services is straight forward and supports rapid development.

Download

ServiceServer.phps

ServiceClient.phps

ExampleServer.phps

ExampleClient.phps

Written by Kristian Lunde

June 8th, 2008 at 10:28 pm

Get Adobe Flash player