Remote Procedure Calls with PEAR::XML-RPC - Client Sending
(Page 5 of 12 )
We start by requiring the use the PEAR package XML/RPC.php. The $first and $last variables remain unchanged, but we store the name of the remote function as $function. The $number variable will serve the same purpose as before but will appear later in our script.
<?php require_once("XML/RPC.php");
$first = "George"; $last = "Washington";
$function = "getNameLength"; ?> |
The XML-RPC specification was designed for use by a variety of languages; while PHP is a loosely typed language we are still required to pass the value's type. Valid data types are: int, double, string, boolean, array, struct, dateTime.iso8601 and base64.
We define each parameter to be passed using XML_RPC_Value that accepts a value and it's type. The $params array simply gathers the resulting objects into an array.
<?php $p1 = new XML_RPC_Value($first, "string"); $p2 = new XML_RPC_Value($last, "string"); $params = array($p1, $p2); ?> |
An XML message object is constructed by passing the function name and the array of parameters to XML_RPC_Message.
<?php $message = new XML_RPC_Message($function, $params); ?> |
In order to send the message we must be able to locate the remote server. A connection is established by using XML_RPC_Client and passing the server interface, the remote server's address and the designated port number through which communications will travel. Again, the most common transport mechanism used for RPC is the HTTP protocol and so we use port 80.
Finally, the message is sent by invoking the send method on the server object. The method accepts the message object and returns an XML response object which we store as $response.
<?php $client = new XML_RPC_Client("/RPC2.php", "localhost", 80); $result = $client->send($message); ?> |
The serialize method can be used at any time on either a message or response object to view the data in context of its XML wrappings.
<?php echo "<p><b>Function Call:</b> $function($first, $last)</p>"; echo "<p><b>Sent XML Message:</b>"; echo "<pre>" . htmlentities($message->serialize()). "</pre></p>"; ?> |
Next: Client Receiving >>
More Miscellaneous Articles
More By bluephoenix