Silverlight & PHP

Silverlight 2 Beta 2 enables significantly improved interop with SOAP based web-services. Web service proxy class end-point URLs can now be configured without recompiling applications. Visual Studio also now has a new “Silverlight-enabled WCF Service” project item template that you can add to ASP.NET web projects to publish services to clients.

It is uncommon to use Silverlight in conjunction with PHP, but sometimes you may need this. In this post I will show you how easy it can be using Silverlight SOAP based web-services support. Actually it is really very simple. All we need is a PHP web service. Than we just add a Service reference to our Silverlight app, and voila.

There are a few ways to create a PHP SOAP web service. In this example I will use NuSOAP library. The NuSOAP project is hosted by SourceForge. NuSOAP is a group of PHP classes that allow developers to create and consume SOAP web services. It does not require any special PHP extensions. The current release version (0.7.3) of NuSOAP supports SOAP 1.1 specification. It can generate WSDL 1.1 and also consume it for use in serialization. Both rpc/encoded and document/literal services are supported.

Creating the Web Service

Our web service will return a list of employees in XML format. So let’s begin. First, create a file, named EmployeeService.php. This file will hold the web service. We need to create a function, which returns a list of employees.

The result of this function looks like this:

Then we need to create & configure the SOAP server, which will hold the service. Using NuSOAP lib this is very easy.

$ns is the namespace of our service. The register method registers a function to our web service. You can either register an oridinary function or a function inside a class. The first parameter is the name of the function, the second one is an array of the input parameters, the third parameter is an array of the returned values and the last one is the namespace. For each parameter (input or output) you should specify name and type. Finally, you have to call service method, which processes the service. You can read more about NuSOAP here.

Our web service is ready to be consumed. You can view it capabilities here. Now we are going to create the Silverlight client. Let’s name it PHPLight.

Using the Web Service

PHPLight Create New Project

The design is very simple. We have just a ListBox to show the employees. Now we have to add a Web Service reference to our project.

When you do this, Add Service Reference dialog appears. In the address field you type the URL of the web service. Notice, you have to write the path to the WSDL of your service. In our case (and in most of cases) you have to add ?wsdl after the service address. When you press GO button, the wizard gets the content of your service. Let’s change the namespace of our service EmployeeService and press OK.

Visual Studio will create the necessary helper files for you. Now we have to add a few other references to our project. First of them is a reference to Xml Linq. We will use it to manipulate the returned XML data from the service. The second one is a reference to Xml Serialization. When you add a web service reference it creates a file, named Reference.cs. This file uses XmlSerializerFormatAttribute. If you don’t add this reference, you get the following errors:

  • ‘System.ServiceModel.XmlSerializerFormatAttribute’ is inaccessible due to its protection level
  • The type or namespace name ‘XmlSerializerFormatAttributeAttribute’ does not exist in the namespace ‘System.ServiceModel’ (are you missing an assembly reference?)

There is something else that you should do to enable your service working properly. When you add a reference to Xml Serialization, you get another error, which says:

  • ‘System.ServiceModel.XmlSerializerFormatAttribute’ does not contain a definition for ‘Use’

You should open Reference.cs file and change the following line:

to

Now all you have to do is to call the web service. In Loaded event of the UserControl write the following:

As you can see, the calls to the web service are made asynchronously. When you make calls to a component, which won’t return the results immediately, you must put the calls in another thread. In getEmployeesCompleted method we will parse the results and pass it to the ListBox.

We are ready! Here is the result of our work.

[ Online demo ] [ Project Source ]