Representational State Transfer (REST) architectural style for distributed hypermedia systems can be a new model for web services construction. REST is not a standard, because REST is just an architectural style, you can't bottle up that style, you can only understand it, and design your Web services in that style.
The REST web services architecture is related to the Service Oriented Architecture. This limits the interface to HTTP with the four well-defined verbs: GET, POST, PUT, and DELETE. REST web services also tend to use XML as the main messaging format.
In this post we want to illustrate how simple and efficient are an implemantation of a web service using Zend Framework and Flex to represent the data retrieved.
The Zend Framework provides both REST Client and Server capabilities. The Server component features automatic exposition of functions and classes using a meaningful and simple XML format. When accessing these services using Flex, it is possible to easily retrieve the return data from the remote call and will still provide easier data access.
You must have installed the Zend Framework and configured for your web server, you can find instrunctions on the Zend website.
On the server side you must provide a php function to call from Flex like this, in this example we provide the sayHello function:
<?php>
require_once 'Zend/Rest/Server.php';
/**
* Say Hello
*
* @param string $who
* @param string $when
* @return SimpleXMLElement
*/
function sayHello($who, $when)
{
$xml ='<?xml version="1.0" encoding="ISO-8859-1"?>
<mysite>
<value>Hey $who! Hope you're having a good $when</value>
<code>200</code>
</mysite>';
$xml = simplexml_load_string($xml);
return $xml;
}
$server = new Zend_Rest_Server();
$server->addFunction('sayHello);
$server->handle();
</php>
On the Flex side we must provide an HTTPService that point to the http://yourSite/restClass.php, and providing the necessary parameter for sayHello function, who and when, and also we must provide the method parameter that represent the function to invoke.
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="{myRestService.send()}">
<mx:HTTPService
id="myRestService"
url="http://yourSite/restClass.php"
result="myResult(event)">
<mx:request>
<method>sayHello</method>
<who>Boy</who>
<when>day</when>
</mx:request>
</mx:HTTPService>
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private function myResult(event:ResultEvent):void {
Alert.show( String ( event.result.mysite.value ) );
}
]]>
</mx:Script>
</mx:Application>
This is the simple step to implement a REST web services using the Zend Framework.
Zend Framework also provide a lot of features like a cache system to improve performance of your query.