An introduction to XML-RPC in PHP
(Page 2 out of 2)A simple XML-RPC client
If you have a look at the manual again, you'll see that all it takes to query a web service is the following code:
include ('xmlrpc.php');
// Create new XML-RPC client
$client = new IXR_Client('http://projects/phpit/content/introduction%20xml-rpc%20in%20php/demos/server1.php');
// Try the sayHello function
if (!$client->query('demo.sayHello')) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
}
// Show response
echo $client->getResponse();
?>
Is that easy or what?! What the above code does is first create the XML-RPC client, and passes the URL of the server as an argument. As you can see I'm using a server that's located on my own computer (http://projects points to a folder on my computer). After that the code sends the query ($client->query), and immediately checks if there weren't any errors. If everything is alright, it displays the response of the server.
Let's write a simple client that queries a web service running on PHPit, which simply returns the number of times it's been queried:
include ('xmlrpc.php');
// Create new XML-RPC client
$client = new IXR_Client('http://phpit.net/demo/introduction%20xml-rpc%20in%20php/phpitserver.php');
// Get query count
if (!$client->query('phpit.getCounter')) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
}
// Show response
echo 'Number of times queried: ' . $client->getResponse();
?>
The above code is very similar to our previous code, and uses the "phpit.getCounter" method to get the number of times the web service has been queried.
Multiple queries at once
In the previous examples we've only sent one query, but it's possible that you want to send multiple XML-RPC queries in one script, which can be done using the following code:
if (!$client->query('demo.sayHello', 'Dennis')) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
}
// Show response
echo $client->getResponse();
echo '';
// Now try adding up two numbers
if (!$client->query('demo.addTwoNumbers', 4, 5)) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
}
echo 'Result: ' . $client->getResponse();
echo '';
But there is one big disadvantage to the above code, because of the way the internet works. Even though both requests are very small in size (not even a kilobyte), it still carries overhead, because each query is a separate HTTP request, which means a connection must be made for each query. This can easily add up to an extra half a second of load time, which is clearly unacceptable for any PHP script.
That's why the XML-RPC library comes with built-in 'multicall' support, allowing you to send multiple queries at once, but only if the server supports it. The code to do a multicall query looks like this:
include ('xmlrpc.php');
// Create new XML-RPC client
$client = new IXR_ClientMulticall('http://projects/phpit/content/introduction%20xml-rpc%20in%20php/demos/server1.php');
// Add method calls
$client->addCall('demo.sayHello');
$client->addCall('demo.addTwoNumbers', 3, 4);
// Send query
if ($client->query()) {
$response = $client->getResponse();
} else {
echo "Error! "
.$client->getErrorCode().":".$client->getErrorMessage().'';
}
// Show response
echo ''
;
print_r ($response);
echo '';
?>
As you can see the above code sends two queries to my own server, and then shows both responses. Since you're doing multiple queries at once, each response of each query is located in a new array item.
That's it for XML-RPC clients, and thanks to the XML library, it takes very little code.
Conclusion
In this tutorial I have taken you through the steps necessary to create your own XML-RPC server and client. Without the XML-RPC library by Simon Willison, it'd would've been 10x times harder, but thanks to his library, most of the hard work is done for us.
I hope I've inspired you to do some really neat things, and if you have any interesting uses of XML-RPC, please e-mail me at dennis [A - T] PHPit [dot] net. There are so many things you can do, and there are already so many public web services (see the list at XMLRPC.com).
Click here to download all the demos in this tutorial.
If you have any comments on this tutorial drop them below, or if you would like to discuss it further, join us at PHPit Forums to talk about anything PHP.
January 30th, 2006 at 10:55 am
Great tute, have been meaning to investigate xml-rpc for some time but never got round to it, this tute has re awoke my interest whilst i read it over my morning coffee…
Thanks ;)
January 31st, 2006 at 12:06 pm
Thanks for the tutorial mate.
February 3rd, 2006 at 3:05 pm
[…] Link: PHPit - Totally PHP » An introduction to XML-RPC in PHP […]
February 18th, 2006 at 7:41 pm
thanks for this tutorial, i will use xml-rpc to incorporate a web appication based on php to the CMS joomla, this tuto is very useful
thanks ;-)
February 22nd, 2006 at 11:08 am
Nice tutorial, very useful,
any chance on how to send an XML file to a .NET webservice?
February 28th, 2006 at 9:52 pm
[…] phpit […]
March 6th, 2006 at 1:24 pm
[…] PHPit - Totally PHP » An introduction to XML-RPC in PHP (tags: development php rpc xml) […]
March 20th, 2006 at 7:11 pm
[…] PHP bietet einige Funktionen zu XML-RPC, aber die XML-RPC Library for PHP macht es wesentlich komfortabler. Außerdem gab es dazu diese schöne (englische) Anleitung, in der beispielhaft ein Client und ein Server entwickelt werden. […]
March 23rd, 2006 at 2:23 pm
[…] An introduction to XML-RPC in PHP http://phpit.net/article/introduction-xml-rpc-php/ 给exblog添加xml-rpc功能的idea来自这篇文章,闲逛时看到的 […]
April 10th, 2006 at 5:50 am
PHPit - Totally PHP » An introduction to XML-RPC in PHP
Here’s an excellent article from Dennis Pallett at PHPit on how to implement an XML-RPC server and client using PHP.
“XML-RPC web services are commonly included with blogging tools (such as WordPress) and content management systems, but man…