Introduction To XML-RPC In PHP

Let us get familiar with some terminology.

RPC From the Wikipedia:
"Remote procedure call (RPC) is an Inter-process communication technology that allows a computer program to cause a subroutine or procedure to execute in another address space (commonly on another computer on a shared network) without the programmer explicitly coding the details for this remote interaction."

Good definition. But what exactly does it mean?

You write functions in your program to reuse code and various other reasons. When your functions are capable of doing cool things, you use it in more than one application.

Imagine, you wish to create a website to publish latest football match scores. You decide to scrape information from a well known football publishing website programatically and display them on your site. You write the scripts and launch the website. You know how difficult it is to scrape information from other sites when they are not prepared to be used in such a fashion. You might be writing adhoc regular expressions. The contents on site from which you are scraping may vary. Finally you reach a point where it is nearly impossible to maintain such a website. But you really wish there was a way to workaround this.

Enter RPC. Wouldn't it be cool to call functions from other websites? Of course yes. RPC is a way to call procedures(functions) on remote computer.

But which programming language does the other website use? How will you actually call the functions from other websites?

Enter XML-RPC. From the Wikipedia:
"XML-RPC is a remote procedure call protocol which uses XML to encode its calls and HTTP as a transport mechanism."

If the publisher of the other website decides to expose procedures(functions) over HTTP, they can use a technology like XML-RPC. XML-RPC is a spec that describes how to receive calls and return responses using XML and HTTP.

Using XML instead of HTML makes communication between two computers easy. XML was designed to transport and store data. Almost all modern programming languages have XML parsers. PHP has very good support to parse, create and manipulate XML documents. The terms method, function and procedure are equivalent in this context.

XML-RPC removes the platform and programming language barriers. The XML-RPC server may be written in Java on a Unix server. You can write a XML-RPC client to talk to that server in any language, say PHP. Your client may run on any operating system of your choice. Technologies like XML-RPC offer a great deal of interoperability between computer systems.

Let us write a sample XML-RPC client in PHP. betty.userland.com is an XML-RPC server that provides few example implementations. It has a method examples.getStateName that returns a US state name based on the parameter you send. For example, it returns "Idaho" if you send 12 as the parameter to the method examples.getStateName.

Let us begin coding our first XML-RPC client.

In our example, we are going to use the PHP XMLRPC extension. Make sure the extension is loaded before we proceed.

<?php
echo extension_loaded('xmlrpc');
?>

Now that we know we have to talk to betty.userland.com over HTTP using XML, let us create the XML request document.The PHP function xmlrpc_encode_request does it for us.

<?php
$request 
xmlrpc_encode_request("examples.getStateName"12);
?>

If you echo the $request variable, you will see the XML we need.

<?xml version="1.0" encoding="iso-8859-1"?>
<methodCall>
<methodName>examples.getStateName</methodName>
<params>
 <param>
  <value>
   <int>12</int>
  </value>
 </param>
</params>
</methodCall>

The request XML document indicates we are calling the remote method examples.getStateName. We are also passing the integer parameter 12 meaning we want to know the US state name 12.

Next, we create a PHP steams context and store it in the variable $context.

<?php
$context 
stream_context_create(array('http' => array(
    
'method' => "POST",
    
'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
    
'content' => $request
)));
?>

Looking at the code snippet, one can easily understand that we are preparing to make an HTTP POST request. We are also setting the appropriate headers and the request XML to the context.

Mention the URL of the XML-RPC server.

<?php
$server 
'http://betty.userland.com/RPC2';
?>

Make the XML-RPC call.

<?php
$file 
file_get_contents($serverfalse$context);
?>

The variable $file now contains the response XML document. If you echo $file you will see the XML like:

<?xml version="1.0"?>
<methodResponse>
	<params>
		<param>
			<value>Idaho</value>
			</param>
		</params>
	</methodResponse>

The server responded successfully to our request. We see the value "Idaho" in the response XML. Finally, we retrieve the value to a PHP variable.

<?php
$response 
xmlrpc_decode($file);
?>

If you echo the variable $response, you will see the state name "Idaho".

We have successfully written an XML-RPC client in only six statements. The entire script is posted below.

<?php
$request 
xmlrpc_encode_request("examples.getStateName"12);

$context stream_context_create(array('http' => array(
    
'method' => "POST",
    
'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
    
'content' => $request
)));

$server 'http://betty.userland.com/RPC2';

$file file_get_contents($serverfalse$context);
$response xmlrpc_decode($file);
echo 
$response;
?>

Try changing the integer parameter to any number between 1 and 50 and see what happens.

You can also grab the code from the Code Album github.com repository.

If this article helped you understand writing XML-RPC client in PHP, you may wish to proceed to the article XML RPC Server And Client In PHP - Determine Client's IP Address.

Taxonomy upgrade extras: 

Comments

Awesome....

I love it

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.