Quite simply it is a way for two computers to communicate over the
internet using XML. One computer, which we will call the client, sends
an XML-RPC request to another computer, which we will call the
server. Once the server receives and processes the request it will send
back a response to the client.

For example, using the MetaWeblog API, an XML-RPC Client (usually a
desktop publishing tool) will send a request to an XML-RPC Server
running on your site. This request might be a new weblog entry being
sent for publication, or it could be a request for an existing entry for
editing. When the XML-RPC Server receives this request it will examine
it to determine which class/method should be called to process the
request. Once processed, the server will then send back a response
message.

The above code initializes the XML-RPC class, sets the server URL and
method to be called (weblogUpdates.ping). The request (in this case, the
title and URL of your site) is placed into an array for transportation,
and compiled using the request() function. Lastly, the full request is
sent. If the send_request() method returns false we will display the
error message sent back from the XML-RPC Server.

An XML-RPC request is simply the data you are sending to the XML-RPC
server. Each piece of data in a request is referred to as a request
parameter. The above example has two parameters: The URL and title of
your site. When the XML-RPC server receives your request, it will look
for parameters it requires.

Request parameters must be placed into an array for transportation, and
each parameter can be one of seven data types (strings, numbers, dates,
etc.). If your parameters are something other than strings you will have
to include the data type in the request array.

An XML-RPC Server acts as a traffic cop of sorts, waiting for incoming
requests and redirecting them to the appropriate functions for
processing.

To create your own XML-RPC server involves initializing the XML-RPC
Server class in your controller where you expect the incoming request to
appear, then setting up an array with mapping instructions so that
incoming requests can be sent to the appropriate class and method for
processing.

The above example contains an array specifying two method requests that
the Server allows. The allowed methods are on the left side of the
array. When either of those are received, they will be mapped to the
class and method on the right.

The ‘object’ key is a special key that you pass an instantiated class
object with, which is necessary when the method you are mapping to is
not part of the CodeIgniter super object.

In other words, if an XML-RPC Client sends a request for the new_post
method, your server will load the My_blog class and call the new_entry
function. If the request is for the update_post method, your server
will load the My_blog class and call the update_entry() method.

The function names in the above example are arbitrary. You’ll decide
what they should be called on your server, or if you are using
standardized APIs, like the Blogger or MetaWeblog API, you’ll use their
function names.

There are two additional configuration keys you may make use of when
initializing the server class: debug can be set to TRUE in order to
enable debugging, and xss_clean may be set to FALSE to prevent sending
data through the Security library’s xss_clean() method.

When the XML-RPC Server receives a request and loads the class/method
for processing, it will pass an object to that method containing the
data sent by the client.

Using the above example, if the new_post method is requested, the
server will expect a class to exist with this prototype:

classMy_blogextendsCI_Controller{publicfunctionnew_post($request){}}

The $request variable is an object compiled by the Server, which
contains the data sent by the XML-RPC Client. Using this object you will
have access to the request parameters enabling you to process the
request. When you are done you will send a Response back to the Client.

Below is a real-world example, using the Blogger API. One of the methods
in the Blogger API is getUserInfo(). Using this method, an XML-RPC
Client can send the Server a username and password, in return the Server
sends back information about that particular user (nickname, user ID,
email address, etc.). Here is how the processing function might look:

Similar to Requests, Responses must be formatted as an array.
However, unlike requests, a response is an array that contains a
single item. This item can be an array with several additional arrays,
but there can be only one primary array index. In other words, the basic
prototype is this:

$response=array('Response data','array');

Responses, however, usually contain multiple pieces of information. In
order to accomplish this we must put the response into its own array so
that the primary array continues to contain a single piece of data.
Here’s an example showing how this might be accomplished:

To help you understand everything we’ve covered thus far, let’s create a
couple Controlladores that act as XML-RPC Client and Server. You’ll use the
Client to send a request to the Server and receive a response.

Using a text editor, create a controller called Xmlrpc_client.php. In
it, place this code and save it to your application/Controlladores/
folder:

<?phpclassXmlrpc_clientextendsCI_Controller{publicfunctionindex(){$this->load->helper('url');$server_url=site_url('xmlrpc_server');$this->load->library('xmlrpc');$this->xmlrpc->server($server_url,80);$this->xmlrpc->method('Greetings');$request=array('How is it going?');$this->xmlrpc->request($request);if(!$this->xmlrpc->send_request()){echo$this->xmlrpc->display_error();}else{echo'<pre>';print_r($this->xmlrpc->display_response());echo'</pre>';}}}?>

Note

In the above code we are using a “url helper”. You can find more
information in the Helpers Functions page.

You should now see the message you sent to the server, and its response
back to you.

The client you created sends a message (“How’s is going?”) to the
server, along with a request for the “Greetings” method. The Server
receives the request and maps it to the process() method, where a
response is sent back.