HomeASP.NET Enhancing PHP Programming with the ASP.NET...
Enhancing PHP Programming with the ASP.NET AJAX Framework
In this article, I will teach you how to introduce the Microsoft ASP.NET AJAX client-side framework into PHP programming by digging into its inner workings. I will also provide a typical sample application.
Contributed by Xianzhong Zhu Rating: / 4 June 24, 2008
As most people know, the ASP.NET AJAX framework is composed of three components: ASP.NET 2.0 AJAX Extensions, ASP.NET AJAX Control Toolkit, and ASP.NET Futures CTP. The first two components rely heavily on the ASP.NET 2.0 server-side controls. In essence, the two parts are mainly designed to extend the Ajax characteristics of ASP.NET 2.0 (as well as later versions).
In contrast, ASP.NET Futures CTP (formerly called 'ASP.NET AJAX Futures CTP') is rather loosely coupled with the ASP.NET server side controls, while still mainly used for the ASP.NET-based web applications. The main business logic is shifted to the client side in order to drastically improve the overall performance of the system.
On the other hand, there is plenty of JavaScript source code accompanying the above three components, most of which is provided by the ASP.NET 2.0 AJAX Extensions and ASP.NET Futures CTP. The most important thing is that a good portion of the JavaScript code can be taken out and put into use in other kinds of server side web development platforms.
Take MicrosoftAjax.js, MicrosoftAjaxTimer.js, and MicrosoftAjaxWebForms.js; you will easily see that they are made up of independent JavaScript code, with merely a copy of the code embedded inside the assembly System.Web.Extensions.dll. In other words, these scripts are independent of ASP.NET server side. Therefore, it's probable that we can leverage these scripts under other server-side web development environments, such as JSP, PHP, and Perl. Moreover, while programming these server-side techniques, we can make much use of the features that come with the ASP.NET AJAX framework, such as the ASP.NET AJAX script features and more advanced client-side characteristics.
To be honest, to achieve the functionalities of ASP.NET AJAX and even the functionality of the ASP.NET AJAX server control ScriptManager, we have to use other non-ASP.NET techniques to make further and even arduous simulating development.
In this article, we are going to explore how to utilize the web service functionality provided by the ASP.NET AJAX client-side framework under PHP environments by dint of the open source project "PHP for Microsoft AJAX Library." At present, however, the project PHP for Microsoft AJAX Library is still in its infancy and it seems a bit early to leverage it in practical scenarios. We can therefore appreciate how to combine PHP with the Microsoft AJAX Library beforehand in order to use its Ajax feature.
In January 2007, Steve Marx of Microsoft released an open source project on Codeplex (http://www.codeplex.com/phpmsajax) that tried to introduce the Microsoft AJAX Library of the ASP.NET AJAX framework into the PHP scenario.
First of all, let's dissect the basic implementation principle of the PHP for Microsoft AJAX Library project.
Up to now, PHP for Microsoft AJAX Library only contains two PHP files: MSAjaxProxyGenerator.php (5K) and MSAjaxService.php (4K). In MSAjaxService.php, there's a PHP class that is defined-MSAjaxService-from which we can further derive custom PHP classes to simulate web service and related web method definitions using PHP skills. Within the other file, MSAjaxProxyGenerator.php, there's another PHP class that is defined-MSAjaxProxyGenerator-through whose generateClientProxy() method (and recurring to Microsoft AJAX Library) the JavaScript proxy of the above-mentioned simulated web service can be implemented without any difficulty.
Before delving into the related sample applications, let's first take a quick look at how to set up the required test circumstances.
Download and set up PHP for Microsoft AJAX Library
Launch your browser and navigate to http://www.codeplex.com/phpmsajax. This is the supporting site for the open source project, PHP for Microsoft AJAX Library. It has the introduction of the project, the latest project version (3 Alpha for downloading), running environment guidance, as well as a basic sample to use the project.
Figure 1-download the open source project, PHP for Microsoft AJAX Library
As is shown in Figure 1, there are two choices for you to download: phpmsajax.zip or phpmsajax-NoSource.zip. Note that the latter merely provides two related PHP files for PHP for Microsoft AJAX Library: MSAjaxProxyGenerator.php and MSAjaxService.php. The former also ships with two sample applications (HelloWorld and ClassSerialization) along with the other two files in order to demonstrate the basic use of the project.
To gain a quicker and better understanding of the project, you are highly advised to download the former. However, after you've looked through the two samples and taken a quick glimpse at the two related project files, you may find out that the whole thing is still in its early stages.
As is explained on the home page of PHP for Microsoft AJAX Library, we have to install PHP 5.2 or higher and make sure we have the php-json module installed. In fact, the home page has also provided the download address for the php-json module (http://www.aurore.net/projects/php-json/). Moreover, on the home page, Steve announced that he had tested the project under both IIS7 (Vista) and Apache (Ubuntu 6.06 + PHP 5.2) environments.
Fortunately, in recent years, PHP and Apache web server have both been developing rapidly and have now released their formal and steady versions-PHP 5.25 and Apache 2.28. Therefore, what you need to do is just download the two and make corresponding installations according to your own operation systems. Everything should go smoothly.
Since my test environment is Windows XP professional + Visual Studio 2005 + ASP.NET AJAX + IIS 5.1 (shipped within Windows XP), the first thing that comes to mind is to test PHP for Microsoft AJAX Library project on IIS 5.1. Regrettably, I downloaded and tried PHP 5.20, PHP 5.23, and PHP 5.25 and tested the two shipped samples (even including some other limited samples found on the Internet) on IIS 5.1 again and again; I failed every time with similar fault prompts, as shown in Figure 2.
Figure 2-the sample projects failed when tested on IIS 5.1
For now, I've concluded that IIS 5.1 leads to the 'strange' error-the version is so low that it results in the error message shown in Figure 2. However, I am sorry not to have given the test result with a higher version of IIS because I was low on time. Therefore, readers who have installed higher versions of IIS can continue with the test.
Finally, I had to resort to the open source Apache 2.2.6-actually, everything goes smoothly!
If you install PHP 5.20, you also have to download the related php-json module, because the project will use the JSON technique to transfer data between the client side and the PHP server side. On the other hand, PHP 5.23 and PHP 5.25 have all provided built-in support for JSON (i.e. not with independent forms of extension modules any more). Therefore, it's better to utilize the higher versions of PHP.
Next, let's turn to another task-download Microsoft AJAX Library.
Download Microsoft AJAX Library
This step is pretty easy. What you need to do is open your browser and navigate to http://www.asp.net/ajax/downloads/. Figure 3 gives the related download snapshot.
Figure 3-download Microsoft AJAX Library
In reality, when you download the package, you will find that the script files are practically the same as those shipped with ASP.NET 2.0 AJAX Extensions.
Now, with all of our prerequisites taken care of, let's delve into a concrete sample that leverages PHP to the Microsoft AJAX Library in order to add Ajax features to the current PHP project. This sample application will retrieve animal information asynchronously from the PHP server side via a simulated web service.
First, we should define a typical PHP class, named Animal, that bears the responsibility of serving as the OOP wrapper for the server side data in database form. However, for simplicity, we have not introduced the database (typically MySQL) related stuff. Here is the complete definition of the class,Animal (the file is Animal.php).
// class Animal
class Animal
{
//define a static array to hold data
static $data = array();
public $Name;
public $Category;
public $Color;
//constructor
public function __construct()
{
@list(
$this->Name,
$this->Category,
$this->Color) = func_get_args();
}
//the main method to return animal related data
public function GetAllAnimals()
{
if (count(Animal::$data) == 0)
{
//populate data into the array in hardcode mode
Animal::$data[] = new Animal("Felix", "Cat", "Grey");
Animal::$data[] = new Animal("Fido", "Dog", "Brown");
Animal::$data[] = new Animal("Rover", "Dog", "Brown");
Animal::$data[] = new Animal("Daisy", "Cow", "Black and White");
Animal::$data[] = new Animal("Polly", "Parrot", "Green");
}
return Animal::$data;
}
}
Since the above PHP class is simple, and what really matters does not lie herein, let's continue to delve into the PHP simulated web service class.
For convenient discussion, we listed all the source code for the AnimalService class, as is shown below (the file is AnimalService.php).
<?php
require_once 'MSAjaxService.php';
require_once 'Animal.php';
class AnimalService extends MSAjaxService
{
function GetAllAnimals()
{
return Animal::GetAllAnimals();
}
}
$service = new AnimalService();
$service->ProcessRequest(array('Animal') );
?>
We must refer to MSAjaxService.php because there is a class defined as MSAjaxService, which, in turn, invokes another class, called MSAjaxProxyGenerator (defined in the file as MSAjaxProxyGenerator.php), to finally generate the client-side web service proxy.
Next, we must derive "AnimalService" from the base class, MSAjaxService. This is a must have in all similar conditions. Then, for brevity, we have defined a web method, called GetAllAnimals(). This invokes "Animal's" method, which is called GetAllAnimals(), and this returns an array of the Animal object.
In the last part, we created an instance of theAnimalService web service and assigned it to the $service variable. After invoking the ProcessRequest() method of the instance and passing the seemingly peculiar array, Animal, as the only parameter, all the server side work is done.
As you may have doped out, the secret is behind the ProcessRequest() method of the MSAjaxService class. This method bears many responsibilities, such as generating related client proxy, calling the json_decode() method to decode the posted data from the client side, calling the json_encode() method to encode the initial data in json mode so it can be transferred to the client side, etc.
So, interested readers may do further research into the MSAjaxService.php file to track down more detailed information.
In the next article, we'll switch to the client-side related programming.