Consuming a Web Service with PEAR::XML-RPC
Querying the Web Service is rather easy. Just instantiate the XML_RPC_Client class (providing name, server, and port of the service), then craft a message with all the parameters, and finally send this message to the server. You receive the result of the Web Service call in returnor an error message. The code calls the Web Services from the previous phrase, assuming that it is accessible using http://localhost/xlmrpc-pear-server.php on port 80.
Calling an XML-RPC Web Service (xmlrpc-pear-client.php)
<?php
require_once 'XML/RPC.php';
$a = 47;
$b = 11;
$xmlrpc = new XML_RPC_Client(
'/xmlrpc-pear-server.php',
'localhost',
80
);
$msg = new XML_RPC_Message(
'phrasebook.php.add',
array(
new XML_RPC_Value($a, 'int'),
new XML_RPC_Value($b, 'int')
)
);
$response = $xmlrpc->send($msg);
$result = $response->value();
if (!$response->faultCode()) {
echo "$a + $b = " . $result->scalarval();
} else {
echo 'Error #' . $response->faultCode() . ': ' .
$response->faultString();
}
?>
When you call $xmlrpc->setDebug(1), debugging messages are turned on, making it easier for you to find out what is going wrong. |
For PHP 5 only, a new extension is available, called XMLRPCi (the i stands for improved), that uses libxml2 for the XML-RPC calls. However, there has only been one release yet, with very minimal documentation, so it is hardly in use at the moment. |
|