revjim.net

PEAR::XML_RPC_Server tutorial

While I’ve managed to find lots of PHP XML-RPC information, I’ve
found very little in regard to using PEAR to make an XML-RPC Server.
Here’s a snippet of code to get you started:

// Include the libraries
require_once 'XML/RPC/Server.php';

// This array maps XML-RPC methods to functions.
$map = array('example.getTime' => array('function' => 'gettime'));

/* Creating a new object of this type automatically
 * Parsed the XML-RPC request and handles the
 * response.
 */
$svr = new XML_RPC_Server($map);

// Bye bye
exit;

/* Our function. Mapped to method example.getTime.
 * When called, example.getTime will accept one
 * parameter -- the format to provide the date in.
 * In all cases, this method merely returns the date.
 */
function gettime($p) {

        // Get the first parameter
        $format = $p->getParam(0);

        // Look to see if it was supplied
        if (empty($format)) {
                // In case it wasn't, this is our default.
                $format = 'Y-m-d h:i:s';
        } else {
                // Get the value of the parameter.
                $format = $format->scalarval();
        }

        // Build a return value
        $retval = date($format,time());

        // Return it as a response
        return new XML_RPC_Response(new XML_RPC_Value($retval, "string")); 

}

I used the long versions of all the functions in order to make
certain that you understand what is going on in each step. Functions
like XML_RPC_decode() make life a lot easier and you may want
to consider playing with them a bit.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>