The Sysadmin Notebook  

Sitemap

XML RPC

Using XML RPC to exchange data

XML-RPC is a standard for data exchange using remote procedure calls and XML data. The procedure and data are exchanged in XML format. See the XML-RPC howto.

A Simple Example

Top Bottom

Weblogs.com offer a ping server accessible via XML-RPC. The server is used by blogging software to notify updates to blogs. We can use the Perl XML::RPC module to send a ping to the server using the XML-RPC servers 'weblogsUpdates.ping' method:

use strict;
use warnings;

use XML::Parser;
use XML::RPC;

my $xmlrpcClient = XML::RPC->new('http://rpc.weblogs.com/RPC2');
$xmlrpcClient->call('weblogUpdates.ping','spray', 'http://y-raps.blogspot.com/')
	or die "Unable to contact weblogs:$!";

my $query = $xmlrpcClient->xml_out();
print "$query\n";

my $response = $xmlrpcClient->xml_in();
print "$response\n";

The XML data sent to the server looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
<methodCall>
<methodName>weblogUpdates.ping</methodName>
<params>
<param>
<value>
<string><![CDATA[spray]]></string>
</value>
</param>
<param>
<value>
<string><![CDATA[http://y-raps.blogspot.com/]]></string>
</value>
</param>
</params>
</methodCall>

The response sent from the server looks like this:

We can tidy the response to a human-readable format with XML::Parser:

use strict;
use warnings;

use XML::Parser;
use XML::RPC;

my $xmlrpcClient = XML::RPC->new('http://rpc.weblogs.com/RPC2');
$xmlrpcClient->call('weblogUpdates.ping','spray', 'http://y-raps.blogspot.com/')
	or die "Unable to contact weblogs:$!";

my $response = $xmlrpcClient->xml_in();
my $parser = XML::Parser->new();
$parser->setHandlers(	Start => \&start,
			Char => \&char,
			End => \&end,);
$parser->parse($response);

sub start() {
  my ($parser, $name, %attr) = @_;
  print "$name: \n";
}
sub char() {
  my ($parser, $data) = @_;
  print $data;
}
sub end() {
  my ($parser, $name,  %attr) = @_;
  print "\n" if (lc($name) eq 'value');
}

The output looks like this:

methodResponse: 
params: 
param: 
value: 
struct: 
member: 
name: 
messagevalue: 
Thanks for the ping.
member: 
name: 
legalvalue: 
You agree that use of the Weblogs.com ping service is governed by the Terms of Use found at www.weblogs.com.
member: 
name: 
flerrorvalue: 
boolean: 
0