![]() Join Up! 96817 members and counting! |
|
|||
Using XML: A PHP Developer's Primer, Part 4: XML-RPC, PHP and Javascript
Adam Delves
This is the second half of an article that began last week. If you haven't read the first part, please do so before reading on...
Creating the PHP Server
The PHP RPC server uses the XML_RPC_Server object provided by the PEAR
XML-RPC package. Both this and the EmailValidator object will be
required in the server:
require_once 'email_validator.php';
PHP functions cannot be mapped directly to the RPC server. The function you wish to map must be contained in a special RPC callable function. This function takes a single parameter which will be passed an XML_RPC_Message object when it is called. The function must return an XML_RPC_Response object containing the return value: function strlen_rpc($message)
The params property of the XML_RPC_Message object is an array of XML_RPC_Value objects containing the parameters passed to the function in the RPC call. The next step is to map the function to a dispatch array so that it is ready to pass to the XML_RPC_Server object: $functions = array('strlen' => array('function' => 'strlen_rpc', // this is the name of the php function to map
The index name of each element (strlen in this case) is the name that the RPC client will use to call the function. The element itself is an array containing three items:
@(new XML_RPC_Server($functions, 1));Two functions; emailValidator_validate (which validates the email address and sends the verification email) and emailValidator_verify (which verifies the verification code) will be mapped to the RPC server. PHP - email_validate.php: require_once 'email_validator.php';
A few points to note from the script shown on the previous page:
When comparing the XML-RPC version of the validation script
to the Ajax version, the code structure is somewhat more logical and
easier to follow. It is no longer up to the developer to interpret,
validate and parse the request--this is now handled by the
XML_RPC_Server object.
Using Javascript to Call our
RPC Server
In the original email validation
application, the client side work was done by an Ajax engine. This Ajax engine will
be replaced with an RPC engine which uses the AJ-RPC client to make the
RPC calls to our server. Technically, the client side portion of the
application will still use Ajax to communicate with the server, however
this task has now been delegated to the AJ-RPC client.
The AJ-RPC client has a similar interface to the PEAR XML-RPC client. First an instance of the XMLRPCClient needs to be created and initialized with the URL of the RPC endpoint (n.b: Javascript will only allow communication with a server that resides on the same domain as the page making the request): var client = new XMLRPCClient('rpc_server.php'); Methods can be added to the client in several ways. The easiest way is to call the aqquireFunctions() function. This causes the XMLRPCClient object to make a call to the system.listMethods function, which is provided by default on all XML-RPC servers. This method returns a list of all functions provided by the server: if (! client.aqquireFunctions()) { // incase the call to system.listMethods fails Functions can also be added to the client manually using the addFunction function. Once a function has been added to the client, it can be called as a function of the client, and its parameters passed as usual: try { The code above demonstrates how to call a function synchronously. Synchronous calls should only be used during page load; a slow response from the RPC server will block all other scripts and events on the page. When called synchronously, fault responses are thrown as exceptions. To call an RPC asynchronously, the onresponse event handler of the XMLRPCClient object or individual XMLRPCFunction must be set to a callable function. This function will receive a single argument: an XML_RPC_Response object: client.onresponse = function(response) // set a response handler for all functions The client-side part of the email validation application works in a similar way as the Ajax version and calls all the RPC methods asynchronously. I have therefore detailed only the relevant changes. The entire script can be found in the ZIP file accompanying this article. The init() function which initializes the environment now creates an instance of the XMLRPC client and assigns the onresponse event handlers to the RPCs which are to be called. If the XMLRPCClient object cannot create an instance of an XMLHTTPRequest object, it throws an exception. This is caught by the init function and causes it to stop executing. Javascript:
/* initializes the environment and sets up all variables - called on the onload event of the document body */ The validateAddress function is executed when the text contained in the email address field is changed. It executes the emailVlaidate.validate RPC. The response is handled by the validateCallback function which first checks that the response is not a fault. It then loads the two items in the return value: the validation result and the email ID into two variables. Javascript: function validateAddress() The checking of the verification code is handled by the verifyAddress and verifyCallback functions. The verifyAddress function is executed when the verify button on the form is pressed. It executes the emailValidator.verify RPC which it passes the email ID and verification code typed by the user. The response is received by the verifyCallback function. Notice here how the fault code is checked, as the RPC may return one of several possible faults: Javascript: function verifyAddress() Again, drawing comparison between the original Ajax
validation engine and the RPC version, you'll notice that the code is a
lot cleaner and easier to understand. Conclusion
XML-RPC simplifies the creation of distributed applications
by providing a standard interface by which developers can use to call
procedures on remote machines. The very concept of RPC and similar
technologies fits in with one of the core objectives of all modern
day programming languages: Code Reuse. Why create your own search
facility, when Google does it better? Why implement your own blogging
system, when several sites provide it for you? The adoption of these
XML-based services including RSS, ATOM and RPC by large corporations
such as Google and Microsoft is testament to the fact the web is
evolving. Before using XML-RPC, it is worth noting that it is not without it drawbacks:
Our next article is going to delve deeper in the world of RPC by taking a look at how SOAP and WSDL are used to completely automate RPCs at the two endpoints and how they serve to describe complex data types and the operations that can be carried out on them. Useful Links
|