Getting Started with Silverlight and WebORB for PHP

WebORB for PHP supports invocations of PHP classes from Silverlight client applications. Using WebORB, Silverlight developers can easily integrate client applications with PHP backend without all the complexity associated with the traditional methods Silverlight relies on.

This guide provides an overview of creating a Silverlight project connecting it with a PHP class using WebORB. The result of the walkthrough is a Silverlight application communicating with a PHP object which runs a database query, returns a result subsequently rendered in Silverlight data grid component.

Required software:

  • WebORB for PHP 3.5 or newer
  • PHP 5 or newer
  • Visual Studio 2008 with Silverlight extensions, .NET 3.5
  • Web server configured to run PHP
Product distribution of WebORB for PHP 3.5 or above contains the following directory structure:
┬  WEBORB Installation directory

├── index.php
------- management console point of entry

├── weborb.php
------- main PHP script - place it into the 
│                      same directory where your compiled SWF
are located and adjust paths in the
require_once calls in the file

├── /Services
------- contains deployed 'remotable' PHP classes

├── /examples
------- contains examples shipped with WebORB

├── /Console 
------- contains WebORB Management Console
│     │     
│     ├─ index.php 
----- main console page
│     │     
│     └─ weborb.php 
----- remoting entry point for the console

├── /weborbassets
│     │     
│     └─ /silverlight
│             │    

│             └─ WeborbClient.dll ---- Silverlight client component

└── /Weborb  
------- contains configuration, log and
     │               WebORB for PHP source code
     └─ weborb-config.xml 
----- contains a reference to the
                                /Services folder, as well as other
important weborb configuration 

If you are deploying on a Windows computer with IIS, make sure to grant Read/Write permissions to the IUSR_<machinename> account for the /Weborb folder.

You can verify the installation by running WebORB Management Console included with the WebORB distribution.  Open localhost/[WEBORB INSTALL PATH]/index.php in a browser. When the console is loaded, you can inspect available PHP services using the Management tab or run the examples included with the product.

PHP Service

WebORB for PHP includes several PHP classes pre-deployed for remote access. To make a PHP class available for remote access it must be deployed into the /services folder from the WebORB product distribution. You can see all deployed classes using the WebORB Management Console. To launch the console open index.php from the root of the WebORB installation folder using the following URL: localhost/WEBORB-HOME-PATH/index.php

Click the Management tab, all deployed services will show up in the service browser in the Services sub-tab. In the service browser click Weborb > Examples and select CustomersDataService as shown below:

The source code for the PHP class is shown below:
class CustomersDataService
  public function getCustomers()
    $conx = mysql_connect("localhost", "flexuser", "password");

    if( $conx )
       throw new Exception( "cannot connect to mysql database" );

    if( !mysql_select_db('northwind', $conx) )
        throw new Exception( "cannot select northwind database, make sure to run northwing.sql from /Services/Weborb/tests" );

    $queryResult = mysql_query("SELECT * FROM customers order by CustomerID") or die("Invalid query: " . mysql_error());
    mysql_close( $conx );
    return $queryResult;

The code runs a query against a MySQL database. Before running the code, the database can be created with a SQL script (northwind.sql) located in the [WEBORB-HOME]/Services/Weborb/tests directory. Run the script and continue below.

Back in the server browser tree view select the getCustomers node which represents the method in the CustomersDataService class and click the Invoke button to invoke the method. You should see the result of the method invocation as shown below:

The object displayed in console is the result of the actual PHP method invocation. Expand one of the array items. The structure of individual record in the response should be as shown below:

The response is structured as an array of objects. Each object represents a row in the query result. Object field names correspond to the columns. It is important to know the structure of the response, but it will be more apparent when we develop the client-side code.

Creating a Silverlight Project

The instructions below will guide you through the process of creating and running a weborb-remoting-enabled Silverlight application. The application invokes a method on a PHP class which runs a database query and returns a result.

Using Visual Studio 2008 create a "Silverlight Application" project. This should be a very straight-forward task if you are familiar with Visual Studio 2008. If not, from the main menu select File > New > Project. Assuming you develop with C#, select "Silverlight" under Visual C# in the Project types tree. Enter a name for you project (this guide uses the Silverlight2PHP name as shown below). Click OK to create the project

The next screen provides a choice of either creating a web application to host the Silverlight client or just creating an HTML page for the client which would need to be deployed to a web server. Since you are working with PHP which is already running in a web server, it is recommended to select an option to generate an HTML page. However, that choice also requires that the page is copied to the web server running your PHP script. Make a selection as shown below and click OK:

Modify Page.xaml to contain the following XAML markup:

="400" Height="300">
Grid x:Name="LayoutRoot" Background="White">
RowDefinition Height="30" />
RowDefinition Height="*" />
Button x:Name="LoadDataButton" Content="Run PHP Query"
="0" Click="LoadDataButton_Click" />
my:DataGrid x:Name="CustomersGrid" Grid.Row="1"
="LightBlue" />

Select and right-click "References" in the Solution Explorer panel. Then click "Add Reference..." as shown below:

Using the Browse tab navigate to the WebORB for PHP installation and select WeborbClient.dll from the [WEBORB-PATH]\weborbassets\silverlight folder as shown below:

Click OK to add the reference.

Back in the XAML editor right click anywhere in the XAML markup and select "View Code" as shown below:

Modify the code to look as shown below:
using System;

// import WebORB client namespace

  public partial class Page : UserControl
    // declare a local variable for the server-side service proxy
    private WeborbClient serviceProxy;

    public Page()
      // Initialize service proxy. The first parameter is the URL
      // for WebORB gateway. In this case the URL is relative.
      // WeborbClient will resolve it against the URL the client
      // application is loaded from. Alternatively the URL can be
      // absolute, such as http://host/path/weborb.php. The second
      // argument must be an instance of UserControl. It is used
      // for callbacks to avoid cross-thread data access problems.

      serviceProxy = new WeborbClient( "weborb.php", this );

    // This method is invoked when the user clicks the
    // LoadDataButton button. The method performs a remote
    // invocation of a method in the PHP class.

    private void LoadDataButton_Click( object sender,
                                       RoutedEventArgs e )
      // Create a responder with references to a function invoked
      // when a response from service is available. The responder
      // is a generic type. The generic parameter must be the type
      // WebORB will convert the return value to. In this case,
      // the result of a query from PHP will be automatically
      // converted to a list of Customer objects. The Customer
      // class (shown below) and must contain the same properties
      // as the names of the columns in the query result (see above).

      Responder<List<Customer>> responder;
      responder = new Responder<List<Customer>>(GotCustomers,null);

      // Invoke a remote method. In this case the first argument is
      // the name of the PHP class, second argument is the name of
      // the method. Third argument must be an array of parameters
      // for the method invocation or null if there are no
      // parameters. Finally the last argument is the responder
      // object with references to the callback functions.

      serviceProxy.Invoke( "Weborb.Examples.CustomersDataService",
                           responder );

    // The method is invoked when a response from the remote method
    // invocation is available. The type of the method argument is
    // the same as the generic parameter type of the responder used
    // above.

    private void GotCustomers( List<Customer> customers )
      // assign the collection of customers as the data source of
      // the customers data grid. The data will be automatically
      // rendered as a result of the data binding.

      CustomersGrid.ItemsSource = customers;

  public class Customer
    public string ContactName { get; set; }
    public string ContactTitle { get; set; }

Save the files and compile the project by pressing F6 or Shift+F6.

Once the project is compiled, deploy the HTML page and the XAP file (Silverlight2PHP.html and Silverlight2PHP.xap if you followed this guide) into the WebORB home directory and run the Silverlight client using the following URL: host/WEBORB-HOME-PATH/Silverlight2PHP.html