I l @ ve RuBoard |
An Alternative PHP/XML-RPC Implementation: XML-RPC for PHPBefore XML-RPC support appeared in PHP, Edd Dumbill's XML-RPC implementation was the only real choice for PHP developers working with the protocol. Powerful, flexible, and easy to use, this XML-RPC implementation provides developers with a full-featured alternative to the XML-RPC-EPI extension that first shipped with PHP 4.1.0. Released under the BSD license, it can be downloaded from http://phpxmlrpc. sourceforge .net/. Completely object-oriented in nature, Edd Dumbill's implementation begins with the construction of a fundamental unit: an XML-RPC value, or xmlrpcval , object. This object is then used as the basis for other more complex objects: an XML-RPC request, or xmlrpcmsg, object; and an XML-RPC response, or xmlrpcresp, object. Finally, client and server objects are available to handle data transmission between the two ends of the XML-RPC connection. In order to better understand this, consider Listing 8.9, which demonstrates the construction of an XML-RPC request using these objects.
Listing 8.9 Constructing an XML-RPC Request<?php // include the class definitions include("xmlrpc.inc"); /* create an instance of the xmlrpcmsg object the xmlrpcmsg object constructor requires two parameters: xmlrpcmsg(procedure, array of xmlrpcval objects) the xmlrpcval object constructor also requires two parameters: xmlrpcval(value, type) allowed xmlrpcval types are "i4", "int", "boolean", "double", "string", Listing 8.9 uses two objects: the fundamental xmlrpcval object for argument values; and the xmlrpcmsg object, which uses the xmlrpcval object in the construction of a complete XML-RPC request. Listing 8.10 demonstrates the output. Listing 8.10 An XML-RPC Request<?xml version="1.0"?> <methodCall> <methodName>getRandomQuote</methodName> <params> <param> <value> <string>Churchill</string> </value> </param> </params> </methodCall> Construction of an XML-RPC response is accomplished in a similar manner, except that this time an xmlrpcresp object is used. Listing 8.11 demonstrates how such a response might be constructed . Listing 8.11 Constructing an XML-RPC Response<?php // include class definitions include("xmlrpc.inc"); /* create an instance of the xmlrpcresp object the xmlrpcresp object constructor requires three parameters: xmlrpcresp(return value as xmlrpcval object, fault code, fault string) */ $rpc = new xmlrpcresp(new xmlrpcval("A fanatic is one who won't change his mind and won't Listing 8.12 demonstrates the output of Listing 8.11. Listing 8.12 An XML-RPC Response<methodResponse> <params> <param> <value> <string>A fanatic is one who won't change his mind and won't With message construction out of the way, all that remains is the transmission layer for messages between the client and server. In this implementation, transmission is handled by two additional objects: xmlrpc_server and xmlrpc_client . They provide methods to send XML-RPC requests , receive XML-RPC responses, decode XML-RPC <value> s into native PHP data types, and log debug messages. In order to illustrate how this works, consider Listing 8.13 and Listing 8.14, which re-create the meteorological server and client from Listing 6.26 and Listing 6.27, again bypassing PHP's native XML-RPC extension in favor of this object-based implementation. Listing 8.13 demonstrates the code for the server. Listing 8.13 A Simple XML-RPC Server<?php // include class definitions include("xmlrpc.inc"); include("xmlrpcs.inc"); // create server object // the object constructor is used to map public procedure names to internal function And Listing 8.14 demonstrates the code for the client. Listing 8.14 A Simple XML-RPC Client<html> <head> <basefont face="Arial"> </head> <body> <?php // form not yet submitted // display form if(!$_POST['submit']) { ?> <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST"> <b>City code:</b> <br> <input type="text" name="city" size="4" maxlength="3"> <p> <b>Data format:</b> <br> <input type="Radio" name="format" value="avg" checked>Average only <br> <input type="radio" name="format" value="raw">Raw data <p> <input type="submit" name="submit" value="Go!"> </form> <?php } else { // include class definitions include("xmlrpc.inc"); // construct array of parameters // this is an associative array of the form ("city" => $city, "format" => $format) $params = new xmlrpcval(array("city" => new xmlrpcval($_POST['city'], "string"), As demonstrated previously, both client and server objects come with specific methods to ease the task of transmitting an RPC request and decoding the response. A simple send() method handles most of the work at the client end of the connection, whereas ancillary methods such as scalarval() , arraymem(), and structmem() (designed specifically to manipulate composite XML-RPC data types such as <struct> s and <array> s) make it possible to easily convert these data types to native PHP structures. An interesting item here are the addition of the faultString() and faultCode() methods, which allow developers to check for the presence of an XML-RPC fault in the server-generated response and take appropriate measures to handle this fault. This XML-RPC implementation is one of the more robust and full-featured ones available online. In addition to basic RPC functions, it also includes a very capable introspection API, support for HTTPS transactions, and a number of utility functions for converting between PHP and XML-RPC data types. Finally, because it's been under development for awhile, it's also fairly well-documented; if you plan to use it in one of your projects, you might want to visit this book's companion web site, which offers links to articles and reference material on the topic to help you get started. |
I l @ ve RuBoard |