Logo         Community
The Company
Your Account
Contact Us
Performing Remote Procedure Calls With PEAR XML_RPC
Learn to harness the power of XML-RPC in your PHP scripts

| Service With A Smile |

With the basics out of the way, let's now turn our attention to the meat of the XML_RPC package - the Server and Client objects. Consider the following listing, which demonstrates the process of creating an XML-RPC server:

// include server class
include ('XML/Server.php');

// create server
// set up dispatch map
$server = new XML_RPC_Server(array("whoAmI" => array("function" => "phpWhoAmI")));

With the PEAR XML_RPC class, creating an XML-RPC server is pretty simple - all you need to do is initialize an object of the XML_RPC_Server() class, and pass the object constructor what geeks call a dispatch map. Essentially, a dispatch map maps the server's "public" XML-RPC procedures to "private" PHP functions; every time an XML-RPC procedure is called by a client, the server consults the dispatch map to see which PHP function it should invoke to handle the call.

In the example above, the XML-RPC server exposes a single procedure - whoAmI() - which is internally mapped to the phpWhoAmI() function. This PHP function is responsible for parsing the XML-RPC request packet, extracting the procedure arguments from it, performing its calculations, and encoding the return value into a response packet suitable for transmission to the server. Let's take a closer look, to see how all these tasks are accomplished:

// include server class
include ('XML/Server.php');

// create server
// set up dispatch map
$server = new XML_RPC_Server(array("whoAmI" => array("function" => "phpWhoAmI")));

// function to parse arguments and return them to
// caller in different format
function phpWhoAmI($params)
$name = $params->getParam(0);
$species = $params->getParam(1);
$age = $params->getParam(2);

$response = "I am " . $name->scalarval() . ", a " . $age->scalarval() . "-year old " . $species->scalarval();

// return response to client
return new XML_RPC_Response(new XML_RPC_Value($response, "string"));

From the above, it is clear that three arguments are required by the phpWhoAmI() function - a name, a species type, and an age. These three arguments are extracted as XML_RPC_Value() objects from the request packet via the getParam() method, and their values are retrieved via each object's scalarval() method. Once retrieved, the arguments are interpolated into a longer string, which is then repackaged as an XML_RPC_Response() object, and returned to the caller.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback