Getting started with WebORB for PHP

WebORB for PHP provides a Flex Remoting implementation. Using WebORB, PHP developers can integrate Flex client applications with objects deployed in PHP applications. Additionally, WebORB for PHP can function as a Flash Remoting gateway thus supporting Flash client applications.

This guide provides an overview of creating a Flex client using Flex Builder 2.0 and connecting it with a PHP application. The result of the walkthrough is a Flex application communicating with a PHP object exposed through WebORB for PHP.

GETTING STARTED - WEBORB INSTALLATION

WebORB for PHP installation has the following directory structure:

┬  WEBORB Installation directory

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

├── /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


└── /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 
      ├─ weborb-log.txt    
----- weborb log file
      │
      └─ /WEB-INF
            │     
            └─ /flex
                │     
                ├─ remoting-config.xml
 -- configures Flex destinations
                └─ services-config.xml 
-- configures Flex RPC endpoint
 

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 http://localhost/[WEBORB INSTALL PATH]/index.php in a browser. When the console is loaded, you can inspect available PHP remoting services using the Management tab or run the examples included with the product.

GETTING STARTED - CREATING A FLEX APPLICATION

Start Flex Builder 2.0 and select File -> New -> Flex Project. A dialog window shown below will appear. Make the selections as shown below and click "Next >".

The next step is very important as it establishes the configuration paths. Uncheck the 'Use default location..' checkbox. The 'Root folder' field must contain the path to the /Weborb folder from the WebORB for PHP distribution. The 'Root URL' field must contain a URL pointing to the same /Weborb folder:

 Click "Next >" to continue.

The next step is to assign a name to the Flex project. Enter  "SampleFlexToPHPProject" for the project the name as shown below and click 'Next' to continue.

The final step in Flex project creation is to set the output folder path and output folder url. Both of these values must point to a folder in the web server hierarchy. It is important that both 'Output folder' and 'Output folder URL' point to the same directory.

Click 'Finish' to finalize project construction.

Flex Builder creates an empty Flex application. The steps below will guide through building a Flex application and connecting it with a PHP object.
 

CONFIGURATION - FLEX BUILDER

WebORB for PHP product distribution contains a finished Flex application demonstrating Flex to WebORB connectivity and a remoting invocation. Copy and paste the contents of the example.mxml file located at

     \Examples\SampleApp\

into the mxml file created in Flex Builder. The code in the application connects to a PHP object and retrieves some basic information about the computer where the object is running.
 

CONFIGURATION - WEBORB

Flex applications require a declaration of the exposed classes as "destinations". Destinations must be configured in remoting-config.xml located in the \Weborb\WEB-INF\flex folder. The application in this example uses the "InfoService" destination defined as:

 <destination id="InfoServiceDestination">
   <properties>
     <source>InfoService</source>
   </properties>
</destination>

Flex Builder loads the configuration file during the compile time.

IMPORTANT: Make sure to locate the directory where Flex Builder places the compiled client application. It is very important to place a copy of weborb.php into the same directory. weborb.php must contain the following PHP code:

<?php 
   require_once("../Weborb/ORBHttpHandler.php");

   $m_ORBHttpHandler = new ORBHttpHandler();
   $m_ORBHttpHandler->processRequest();
?>

VERY IMPORTANT: The first line must be edited to point to the Weborb directory from the product distribution. For example, look at weborb.php located in the /Console folder

RUNNING FLEX APPLICATION

When you run the application in Flex Builder, it opens a browser and loads the application from:

http://localhost/Examples/SampleFlexToPHPProject.html

The application connects to the backend service upon the startup or when user clicks the "Send Request" button:
 

CODE REVIEW

Flex application declares a remote object using the RemoteObject API:

remoteObject = new RemoteObject();
remoteObject.destination = "InfoServiceDestination";
remoteObject.getComputerInfo.addEventListener("result", onResult);
remoteObject.addEventListener("fault", onFault);

Notice the destination name matches the destination entered in remoting-config.xml. When a user clicks the 'Get Computer Info' button, the following function executes a remote method invocation:

private function getInfo():void
{
  invokeButton.enabled = false;
  currentUserText.text = "";
  processIdText.text = "";
  osText.text = "";
  phpVersionText.text = "";
  remoteObject.getComputerInfo();
}

When an invocation response is available, Flex invokes a response handler specified in the <RemoteObject> tag. The response handler in the example, populates the text fields with the data available in the returned object:

private function onResult(event:ResultEvent):void
{
  var computerInfo:Object = event.result;
  currentUserText.text = computerInfo.currentUser;
  processIdText.text = computerInfo.phpProcessId;
  osText.text = computerInfo.operatingSystem;
  phpVersionText.text = computerInfo.phpVersion;
  invokeButton.enabled = true;
}

The source code for the server-side object is below:

class InfoService
{
  public function getComputerInfo()
  {
    $compInfo = new ComputerInfo();
    $compInfo->currentUser = get_current_user();
    $compInfo->phpProcessId = getmypid();
    $compInfo->operatingSystem = php_uname( 'a' );
    $compInfo->phpVersion = phpversion();
    return $compInfo;
  }
}

class ComputerInfo
{
  public $currentUser;
  public $phpProcessId;
  public $operatingSystem;
  public $phpVersion;
}