Create an XML-RPC Request with xmlrpc_encode_request

The process to create an XML-RPC request can be broken up into individual steps:

  • Serialize the PHP data into XML-RPC format
  • Create an XML-RPC request by combining HTTP headers and serialized data
  • Make the call to the service

The PHP function xmlrpc_encode_request can be used to serialize data and it creates the entire XML-RPC request call starting with the methodCall element and goes through all the value elements. The xmlrpc_encode_request function requires two parameters. The first parameter is the name of the remote call to request. The methodName element will use this parameter. The second parameter is the variables to pass as parameters. This will be used to create the params structure. xmlrpc_encode_request will automatically detect what kind of variable was passed into it, (variable types, numerical arrays, or associative arrays), and format them into the data type schema dictated by the XML-RPC specifications.

Example code:

<?php
$singleVariable = "Hello!";
$req = xmlrpc_encode_request('NameOfRemoteCall', $singleVariable);
echo $req;
?>

Output of $req:

<?xml version="1.0" encoding="iso-8859-1"?>
<methodCall>
<methodName>NameOfRemoteCall</methodName>
<params>
<param>
<value><string>Hello!</string></value>
</param>
</params>
</methodCall>
Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

 

 
eXTReMe Tracker