Below are several usage examples, showing the full spectrum of options available to developers. Usage examples will each build on the previous example provided.
Example 1045. Basic Usage
The example below attaches a function as a dispatchable XML-RPC method and handles incoming calls.
<?php
/**
* Return the MD5 sum of a value
*
* @param string $value Value to md5sum
* @return string MD5 sum of value
*/
function md5Value($value)
{
return md5($value);
}
$server = new Zend_XmlRpc_Server();
$server->addFunction('md5Value');
echo $server->handle();
Example 1046. Attaching a class
The example below illustrates attaching a class' public methods as dispatchable XML-RPC methods.
<?php
require_once 'Services/Comb.php';
$server = new Zend_XmlRpc_Server();
$server->setClass('Services_Comb');
echo $server->handle();
Example 1047. Attaching a class with arguments
The following example illustrates how to attach a class' public methods and passing arguments to its methods. This can be used to specify certain defaults when registering service classes.
<?php
class Services_PricingService
{
/**
* Calculate current price of product with $productId
*
* @param ProductRepository $productRepository
* @param PurchaseRepository $purchaseRepository
* @param integer $productId
*/
public function calculate(ProductRepository $productRepository,
PurchaseRepository $purchaseRepository,
$productId)
{
...
}
}
$server = new Zend_XmlRpc_Server();
$server->setClass('Services_PricingService',
'pricing',
new ProductRepository(),
new PurchaseRepository());
The arguments passed at setClass() at server construction
time are injected into the method call pricing.calculate() on
remote invokation. In the example above, only the argument
$purchaseId is expected from the client.
Example 1048. Passing arguments only to constructor
Zend_XmlRpc_Server allows to restrict argument passing to
constructors only. This can be used for constructor dependency injection.
To limit injection to constructors, call
sendArgumentsToAllMethods and pass
FALSE as an argument. This disables the default behavior of all
arguments being injected into the remote method. In the example below the instance
of ProductRepository and
PurchaseRepository is only injected into the constructor of
Services_PricingService2.
<?php
class Services_PricingService2
{
/**
* @param ProductRepository $productRepository
* @param PurchaseRepository $purchaseRepository
*/
public function __construct(ProductRepository $productRepository,
PurchaseRepository $purchaseRepository)
{
...
}
/**
* Calculate current price of product with $productId
*
* @param integer $productId
* @return double
*/
public function calculate($productId)
{
...
}
}
$server = new Zend_XmlRpc_Server();
$server->sendArgumentsToAllMethods(false);
$server->setClass('Services_PricingService2',
'pricing',
new ProductRepository(),
new PurchaseRepository());
Example 1049. Attaching a class instance
setClass() allows to register a previously instantiated
object at the server. Just pass an instance instead of the class name. Obviously
passing arguments to the constructor is not possible with pre-instantiated
objects.
Example 1050. Attaching several classes using namespaces
The example below illustrates attaching several classes, each with their own namespace.
<?php
require_once 'Services/Comb.php';
require_once 'Services/Brush.php';
require_once 'Services/Pick.php';
$server = new Zend_XmlRpc_Server();
$server->setClass('Services_Comb', 'comb'); // methods called as comb.*
$server->setClass('Services_Brush', 'brush'); // methods called as brush.*
$server->setClass('Services_Pick', 'pick'); // methods called as pick.*
echo $server->handle();
Example 1051. Specifying exceptions to use as valid fault responses
The example below allows any Services_Exception-derived
class to report its code and message in the fault response.
<?php
require_once 'Services/Exception.php';
require_once 'Services/Comb.php';
require_once 'Services/Brush.php';
require_once 'Services/Pick.php';
// Allow Services_Exceptions to report as fault responses
Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
$server = new Zend_XmlRpc_Server();
$server->setClass('Services_Comb', 'comb'); // methods called as comb.*
$server->setClass('Services_Brush', 'brush'); // methods called as brush.*
$server->setClass('Services_Pick', 'pick'); // methods called as pick.*
echo $server->handle();
Example 1052. Utilizing custom request and response objects
Some use cases require to utilize a custom request object. For example, XML/RPC is not bound to HTTP as a transfer protocol. It is possible to use other transfer protocols like SSH or telnet to send the request and response data over the wire. Another use case is authentication and authorization. In case of a different transfer protocol, one need to change the implementation to read request data.
The example below instantiates a custom request object and passes it to the server to handle.
<?php
require_once 'Services/Request.php';
require_once 'Services/Exception.php';
require_once 'Services/Comb.php';
require_once 'Services/Brush.php';
require_once 'Services/Pick.php';
// Allow Services_Exceptions to report as fault responses
Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
$server = new Zend_XmlRpc_Server();
$server->setClass('Services_Comb', 'comb'); // methods called as comb.*
$server->setClass('Services_Brush', 'brush'); // methods called as brush.*
$server->setClass('Services_Pick', 'pick'); // methods called as pick.*
// Create a request object
$request = new Services_Request();
echo $server->handle($request);
Example 1053. Specifying a custom response class
The example below illustrates specifying a custom response class for the returned response.
<?php
require_once 'Services/Request.php';
require_once 'Services/Response.php';
require_once 'Services/Exception.php';
require_once 'Services/Comb.php';
require_once 'Services/Brush.php';
require_once 'Services/Pick.php';
// Allow Services_Exceptions to report as fault responses
Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
$server = new Zend_XmlRpc_Server();
$server->setClass('Services_Comb', 'comb'); // methods called as comb.*
$server->setClass('Services_Brush', 'brush'); // methods called as brush.*
$server->setClass('Services_Pick', 'pick'); // methods called as pick.*
// Create a request object
$request = new Services_Request();
// Utilize a custom response
$server->setResponseClass('Services_Response');
echo $server->handle($request);




