INCUTIO HOME | SCRIPTS | CONTACT

Manual Index

## XML-RPC with the Incutio PHP Library: A Beginner's Guide

XMP-RPC stands for XML Remote Procedure Calling. It is a protocol for making and receiving procedure calls over the internet.

What this means is that different computers can use XML-RPC to "ask each other questions". Using XML-RPC is just like making a function call in PHP, only the computer that executes the function could be thousands of miles away.

With the Incutio XML-RPC Library, making and receiving XML-RPC requests is almost as simple as calling native PHP functions. Here's some sample code, which calls a function entitled "test.getTime" on our simple demonstration server:

    $client = new IXR_Client('http://scripts.incutio.com/xmlrpc/simpleserver.php');
    $client->query('test.getTime');
    print $client->getResponse();
    // Prints the current time, according to our web server

With error checking, the above code looks like this:

    $client = new IXR_Client('http://scripts.incutio.com/xmlrpc/simpleserver.php');
    if (!$client->query('test.getTime')) {
       die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
    }
    print $client->getResponse();

You can also send arguments along with your queries:

    $client = new IXR_Client('http://scripts.incutio.com/xmlrpc/server.php');
    if (!$client->query('test.add', 4, 5)) {
       die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
    }
    print $client->getResponse();
    // Prints '9'

Arguments are not limited to simple values. You can send strings and arrays as well:

    $client = new IXR_Client('http://scripts.incutio.com/xmlrpc/server.php');
    if (!$client->query('test.addArray', array(3, 5, 7))) {
       die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
    }
    print $client->getResponse();
    // Prints '3 + 5 + 7 = 15'

Writing an XML-RPC server is simple as well. Here's the full code for `simpleserver.php`:

    <?php

    include('IXR_Library.inc.php');

    /* Functions defining the behaviour of the server */

    function getTime($args) {
        return date('H:i:s');
    }

    function add($args) {
        return $args[0] + $args[1];
    }

    function addArray($array) {
        $total = 0;
        foreach ($array as $number) {
            $total += $number;
        }
        return implode(' + ', $array).' = '.$total;
    }

    /* Create the server and map the XML-RPC method names to the relevant functions */

    $server = new IXR_Server(array(
        'test.getTime' => 'getTime',
        'test.add' => 'add',
        'test.addArray' => 'addArray'
    ));

    ?>

Pretty simple huh?

The Incutio XML-RPC Library can do a lot more than is demonstrated above. Be sure to read the manual for more information.

Scripts

- Scripts Home

- PHP XML-RPC Library
- PHP Amazon Search
- PHP Google Web API

- incDirectory
- ssLinks