Flickr.Net – my version

Update: Now storing Flickr.Net source code over here on where a new version is now available for download.

I found the Flickr.Net workspace over at GotDotNet (I’m not linking to the stupid thing as it doesn’t work! what a load of crap it is) which attempts to create a .Net library for accessing the Flickr API.

But basically it only did the upload picture bit, and none of the rest of the API, so I decided to do the lot. So far I’ve written all the code except for the upload bit, as well as a test set and a sample application. I plan on submitting the work either to the Flickr.Net team or something later on.
Continue reading “Flickr.Net – my version”

HTTPS and trusting server certificates

If you making HttpWebRequest calls to secure servers, using HTTPS you’ll probably come across a situation where the server request fails. There can be a number of reasons for this – sometimes you might be using a test certificate which has no trusted root, or your using your own certificate authority which isn’t in the trusted root certificate store on the PC you’re running the tests from. Well you can override the policy, to accept specific certificates, or a blanket cover-all to accept all certificates.

For more information see the MSDN Library page.

Place this somewhere before you start making HttpWebRequest jobs, and define the class.

Then define this class. You can change the CheckValidationResult function to check for different problems, such as expired certificates, untrusted certificate authority etc. Return true if you want to trust the certificate, and false if you don’t want to.

NuSOAP Web service example file

After a few requests I’ve uploaded an example file containing an example web service. This follows most of the guidelines set out in my previous article

Download Zip File

Basically the file splits into 4 parts.

part I – setup

includes the nusoap.php file, sets up the namespace and wsdl object

part II – registering types

registers the types – the singular ones first, then the array types (all using wsdl->addComplexType function)

part III – register the methods

register the various methods to be called using the wsdl->register function.

part IV – define the functions.

finally, define the functions – this can be done in a seperate include file to simplify some of the layout of the file. (why oh why did I want to specify part IV first? damn that Star Wars DVD).

See the wsdl in action

As usual, any questions let me know.

Save bandwidth with PHP caching

I have a php script that generates my list of amazon books, but due to the fact that this generates a lovely delay each time the page loads I set this to be a javascript page, using a document.write to output the html.

This has another side benefit, that I can use browser caching to save on my bandwidth by only refreshing the web service and outputting the whole page if its a old copy.

I decided that I’d only refresh the amazon javascript every day, anytime you see the books listed after that it’ll be a cached copy on your local system.

To implement this you need to be using apache, doesn’t work in IE.

You’re looking for a header called “If-Modified-Since” which the browser will send to see if a new copy is available.

$allheaders = getallheaders();
$if_modified_since = preg_replace(‘/;.*$/’, ”, $allheaders[‘If-Modified-Since’]);
$if_modified_time = strtotime($if_modified_since);

The getallheaders() function is a apache only function. Then you parse this header to get the time and convert to a time integer.

$part = 24 * 60 * 60;
$t = floor(time()/$part)*$part;

if ($if_modified_time > $t) {
   header(“HTTP/1.0 304 Not Modified”);

This takes the current time, and flattens it to the nearest day. Then if the last modified since time if greater than this floored time send back the not modified header and exit.

Then, finally, if this is a ‘new’ file then send the last modified time as the current time.

$gmdate_mod = gmdate(‘D, d M Y H:i:s’, time()) . ‘ GMT’;
header(“Last-Modified: $gmdate_mod”);
header(“Content-Type: application/x-javascript”);

This output the Last-Modified header with the current timestamp. It also output the content-type as javascript, so it is handled properly.

Hope this helps.

Creating a web service and WSDL using NuSOAP

Well if you have followed the previous post on using NuSOAP to create an amazon web service then you might be interested in using NuSOAP to create a web service of your own (i.e. a server component rather than a client component). I decided to create one myself to allow me to create a .Net application to help me maintain my walks database.

Getting Started

As before, go get NuSOAP and download into a directory. Then create a file called “mywsdl.php” for example.

The first thing I did was to set a namespace for my web service. This makes my web service unique and identifiable. The namespace does not have to be a URL but if you make it a URL things can be a lot easier.

Next we need to create the server object and start to program against it:

Creating your data types

There are three main types of data you will be using. Built in types can be used without being declared. The other two are slightly more complicated, structures and arrays.

First lets look at an example structure:

This is quite a complicated function, and from the source code we see that the parameters are as follows:

  • 1: name
  • 2: typeClass (complexType|simpleType|attribute)
  • 3: phpType: currently supported are array and struct (php assoc array)
  • 4: compositor (all|sequence|choice)
  • 5: restrictionBase namespace:name (
  • 6: elements = array ( name = array(name=>”,type=>”) )
  • 7: attrs = array(array(‘ref’ => “”, “” => “string[]”))
  • 8: arrayType: namespace:name (

The first is simply the name you want to define (in the context of the ‘targetNamespace’ defined above.
The next is the type. simpleType is usually based on another type with restrictions (e.g. a string with max length of 4 or some such other restriction), complexTypes usually have more than one subelement (as above).
The phpType, as specified is either array or struct.
The compositor is a way of defining how the subelements of the current element work, are they either all optional element, to be included in any order (all), optional but only to be allowed in the given order (sequence) or only one of them is allowed at any one time (choice).
The restrictionBase and attrs and arrayType we shall cover later when we talk about support for arrays.
The elements item is an array containing a list of subelement, each of which has a name and a type.

The above example defines a type called “Walk” which is effectively a struct, containing 4 fields, WalkId, WalkTitle, WalkDate and WalkDescription. It is a complex type and all elements can be included in any order. WalkId is a integer, WalkTitle and WalkDescription are strings and WalkDate is a date field.

What about list of walks

The next think you might want to do is return a list of walks matching some criteria (e.g. latest 10 walks). For this we use the array data type:

Here the php type is array instead of struct. The compositor is blank and the restrictionBase is ‘SOAP-ENC:Array’. This tells the system that your complex type is based on the Array type defined within the soap-encoding namespace. The elements parameter is then a blank array and the attrs parameter is an array defining the type of the array and the parameter defines the type of the elements within the array (as you can see, these are usually the same except for the [] part after the array definition.

Do some work!

Well so far we’ve defined the types of data (or some extra ones not already defined) that we want to pass in and out of your web service. Next we define the actual operation the web service will support.

Here we define a method which will input a integer, defining the walk id and get passed back a Walk data struct (containing the id, title, description and date). We again define the namespace and I’ve set the action to the namespace + “#GetWalk” which is the method name.

We define a normal php function to actually do the work:

Dates should be in ISO international format CCYY-MM-DD HH:MM:SS with an optional timezone attached at the end (or shorted as in this example).

Finally, we pass whatever data the php page receives into the soap processor and see what happens:

// Use the request to (try to) invoke the service

If you now browse to your completed webpage you should find you are presented with a nice page listing the operations supported and the parameters, as well as a link to the wsdl which can be used with code generation tools (such as wsdl.exe with .Net or the java equivalent) to generate client code for your wsdl.

Comments welcome as usual.