ASPit - Totally ASP
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

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();

?>

(View Live Demo)

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();

?>

(View Live Demo)

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:

// Try the sayHello function
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 '
';

?>

(View Live Demo)

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.

« Previous: XML-RPC Server



10 Responses to “An introduction to XML-RPC in PHP”

  1. Derek Says:

    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 ;)

  2. TuNi Says:

    Thanks for the tutorial mate.

  3. PHPit - Totally PHP » An introduction to XML-RPC in PHP at varun krishnan Says:

    […] Link: PHPit - Totally PHP » An introduction to XML-RPC in PHP […]

  4. sam Says:

    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 ;-)

  5. Riad Afyouni Says:

    Nice tutorial, very useful,
    any chance on how to send an XML file to a .NET webservice?

  6. (take||leave) it » it has been easier Says:

    […] phpit […]

  7. Images of Broken Light » Blog Archive » links for 2006-03-06 Says:

    […] PHPit - Totally PHP » An introduction to XML-RPC in PHP (tags: development php rpc xml) […]

  8. sBLOGet » Blog Archive » Techniken des neuen Web: XML-RPC Says:

    […] 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. […]

  9. PiGPP » XML-RPC相关学习资源 Says:

    […] An introduction to XML-RPC in PHP http://phpit.net/article/introduction-xml-rpc-php/ 给exblog添加xml-rpc功能的idea来自这篇文章,闲逛时看到的 […]

  10. VT's Tech Blog Says:

    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…

Leave a Reply

Article Index
  1. XML-RPC Server
  2. XML-RPC Client
Download Article
PDF
Download this article as a PDF file

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.