ASPit - Totally ASP JSit - Totally JavaScript
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 1 out of 2)

Introduction

In this tutorial you'll learn all about XML-RPC and how to "open" your PHP scripts to the rest of the world, effectively creating a web service.

According to Google a webservice "is any piece of software that makes itself available over the Internet and uses a standardized XML messaging system", which exactly describes XML-RPC. Basically, a web service allows other developers to query your web site, and perform certain tasks or get specific information, without having to go to your website, and this includes desktop applications.

XML-RPC web services are commonly included with blogging tools (such as WordPress) and content management systems, but many other websites benefit from having a public web service, especially web sites that serve data, for example a weather website.

In this tutorial I will show you how to create your own XML-RPC web service, allowing other people to connect to your website. I will also show you how to create your own XML-RPC client, which means you can query other web services.

Although PHP comes with inbuilt XML-RPC functionality, we won't be using it in this tutorial, and instead we'll opt for the excellent XML-RPC Library by Simon Willison, available at http://scripts.incutio.com/xmlrpc/. This library includes both the ability to create a XML-RPC server and client, which is exactly what we need.

Let's start by creating our own XML-RPC server first, and after that we'll create our own client.

Creating a simple XML-RPC server

With the XML-RPC library, creating a XML-RPC server takes very little work, and most of it gets done automatically for you. If you have a look at the manual you will read that all it takes is the following code:

include('xmlrpc.php');

function sayHello($args) {
    return 'Hello!';
}

function addTwoNumbers($args) {
    $number1 = $args[0];
    $number2 = $args[1];
    return $number1 + $number2;
}

$server = new IXR_Server(array(
    'demo.sayHello' => 'sayHello',
    'demo.addTwoNumbers' => 'addTwoNumbers'
));

?>

The above code first includes the XML-RPC library (called xmlrpc.php), and then defines two functions, which will be part of our server. After that it creates the actual server by creating a new instance of the IXR_Server() class, and passing the server functions as an argument in the constructor.

That's basically how the XML-RPC server works, and all you have to do is extend it with your own functions and methods. The methods in the above code are called 'demo.sayHello' and 'demo.addTwoNumbers', but you can call them anything you'd like, e.g. 'myapp.MyFunc' and link them to any function you'd like.

Every server method only accepts one argument, which is an array of arguments passed by the client. For example, if the client sent two arguments to the sayHello method in the above code, the $args variable would be an array consisting of the two arguments, i.e.

Array
(
    [0] => Argument 1

    [1] => Argument 2
)

If there's only one argument, then the $args variable will be that argument.

Let's have a look at creating a XML-RPC server that can describe its methods, by using the IXR_IntrospectionServer() class.

A self-describing server

The XML-RPC library comes with a few standard methods which are built in, and are part of the XML-RPC specification, like for example system.listMethods. This method will return an array to the client of all the methods that are supported by the server, but it won't return any specific details, like the number of arguments each method takes, or its return value.

For this you need the system.methodHelp method, but this is only supported by the IXR_IntrospectionServer() class, and not the regular server class (which we just used). Let's re-create our server, with the IXR_IntrospectionServer class:

include ('xmlrpc.php');

function sayHello($name) {
    return 'Hello, ' . $name;
}

function addTwoNumbers($args) {
    $number1 = $args[0];
    $number2 = $args[1];
    return $number1 + $number2;
}

$server = new IXR_IntrospectionServer();
// Now add the callbacks along with their introspection details
$server->addCallback(
    'demo.sayHello'// XML-RPC method name
    'sayHello',       // function to calback
    array('string', 'string'), // Array specifying the method signature
    'Returns the current date as a string'  // Documentation string
);
$server->addCallback(
    'demo.addTwoNumbers',
    'addTwoNumbers',
    array('int', 'int', 'int'),
    'Adds up two numbers and returns the result'
);

// And serve the request
$server->serve();

?>

This XML-RPC server has the exact same methods, except it now supports the system.methodHelp and system.methodSignature methods. As you can see in the above code, for each method, I've added a small blurb of information, and the 'method signature'. The method signature is an array consisting of at least one item, which describes the type of return value. Optionally, you can also add the types of arguments (which I've done). See the library manual for all the valid types.

The advantage of using this self-describing server, instead of the regular server, is that other people can easily understand what your web service does, without having to read any documentation or FAQ'.s.

Now let's create a simple XML-RPC client that can query any XML-RPC web service, and get ready to be surprised about how easy it is.

Next: XML-RPC Client »



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

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. XML-RPC Server
  2. XML-RPC Client
Bookmark Article
Download Article
PDF
Download this article as a PDF file