XMLRPC: demo.addTwoNumbers

With demo.sayHello, you saw how to make use of wordpress XMLRPC functions. But demo.sayHello doesn’t take any parameters as arguments. To show how to pass arguments, let’s explore demo.addTwoNumbers.

It takes two integers as arguments and returns a response which is the sum of the two numbers


/*The URL of the blog*/
$BLOGURL = "http://localhost/wordpress";

function get_response($URL, $context) {
    if(!function_exists('curl_init')) {
    die ("Curl PHP package not installedn");
    /*Initializing CURL*/
    $curlHandle = curl_init();
    /*The URL to be downloaded is set*/
    curl_setopt($curlHandle, CURLOPT_URL, $URL);
    curl_setopt($curlHandle, CURLOPT_HEADER, false);
    curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);
    /*Now execute the CURL, download the URL specified*/
    $response = curl_exec($curlHandle);
    return $response;

/*Creating the demo.addTwoNumbers request which takes on two parameters
 num1: number 1 
 num2: number 2
 returns the sum
function demo_addTwoNumbers($num1, $num2) {
   global $BLOGURL;
   $request = xmlrpc_encode_request("demo.addTwoNumbers",
      array($num1, $num2));
    /*Making the request to wordpress XMLRPC of your blog*/
    $xmlresponse = get_response($BLOGURL."/xmlrpc.php", $request);
    $response = xmlrpc_decode($xmlresponse);
    /*Printing the response on to the console*/

demo_addTwoNumbers(1, 2);
echo "n";

As you see, that we pass two numbers.

On executing the program
$ php demo.addTwoNumbers.php

<?xml version="1.0"?>


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s