XML-RPC server implementation in PHP - minimal, simplest possible

php, xml-rpc

Solution

I got it.

This is complete, simplest possible XML-RPC Server based on phpxmlrpc library.

<?php

// Complete, simplest possible XMLRPC Server implementation
// file: domain.com/xmlrpc.php

include "lib/xmlrpc.inc";     // from http://phpxmlrpc.sourceforge.net/
include "lib/xmlrpcs.inc";    // from http://phpxmlrpc.sourceforge.net/

// function that handles example.add XML-RPC method 
// (function "mapping" is defined in  `new xmlrpc_server` constructor parameter.
function add ($xmlrpcmsg) 
{
    $a = $xmlrpcmsg->getParam(0)->scalarval(); // first parameter becomes variable $a
    $b = $xmlrpcmsg->getParam(1)->scalarval(); // second parameter becomes variable $b

    $result = $a+$b; // calculating result

    $xmlrpcretval = new xmlrpcval($result, "int"); // creating value object
    $xmlrpcresponse = new xmlrpcresp($xmlrpcretval); // creating response object

    return $xmlrpcresponse; // returning response
}

// creating XML-RPC server object that handles 1 method
$s = new xmlrpc_server(
            array(
                "example.add" =>  array( // xml-rpc function/method name
                    "function" => "add", // php function name to use when "example.add" is called
                    "signature" => array(array($xmlrpcInt, $xmlrpcInt, $xmlrpcInt)), // signature with defined IN and OUT parameter types
                    "docstring" =>  'Returns a+b.' // description of method
                    )          
            )
        );

?>

I dont understand completely what `scalarval()` method does with parameters, I think it just converts object to regular variable/value and it may be used with string.

Problem

Im trying to write simple XMLRPC server in PHP. I've read some documentation and I found minimal implementation, similar to this: ``` // /xmlrpc.php file include "lib/xmlrpc.inc"; include "lib/xmlrpcs.inc"; // function that handles example.add XML-RPC method function add ($xmlrpcmsg) { // ??? $sumResult = $a + $b; // ??? return new xmlrpcresp($some_xmlrpc_val); } // creating XML-RPC server object that handles 1 method $s = new xmlrpc_server( array( "example.add" => array("function" => "add") )); ``` How I can create response? Lets say I want response with one integer `sumResult`, which is sum of `a` and `b` parameters passed to XMLRPC server by XML-RPC call. Im using PHP XML-RPC 3.0.0beta (I can also use 2.2.2, but i decided to use 3.0.0beta because its marked as stable).

Original source