Introduction to Zend\XmlRpc

From its home page, XML-RPC is described as a “…remote procedure calling using HTTP as the transport and XML as the encoding. XML-RPC is designed to be as simple as possible, while allowing complex data structures to be transmitted, processed and returned.”

Zend Framework provides support for both consuming remote XML-RPC services and building new XML-RPC servers.

Quick Start

To show how easy is to create XML-RPC services with Zend\XmlRpc\Server, take a look at the following example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Greeter
{

    /**
    * Say hello to someone.
    *
    * @param string $name Who to greet
    * @return string
    */
    public function sayHello($name='Stranger')
    {
        return sprintf("Hello %s!", $name);
    }
}

$server = new Zend\XmlRpc\Server;
// Our Greeter class will be called
// greeter from the client
$server->setClass('Greeter', 'greeter');
$server->handle();

Note

It is necessary to write function and method docblocks for the services which are to be exposed via Zend\XmlRpc\Server, as it will be used to validate parameters provided to the methods, and also to determine the method help text and method signatures.

An example of a client consuming this XML-RPC service would be something like this:

1
2
3
4
5
6
7
$client = new Zend\XmlRpc\Client('http://example.com/xmlrpcserver.php');

echo $client->call('greeter.sayHello');
// will output "Hello Stranger!"

echo $client->call('greeter.sayHello', array('Dude'));
// will output "Hello Dude!"