xml-rpc-PHP中的xml-rpc服务器实现-尽可能最小、最简单


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

我正在尝试用PHP编写简单的XMLRPC服务器。

我阅读了一些文档,发现了最低限度的实现,类似于以下内容:

// /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")
    ));

如何创建响应

比方说,我想要一个整数sumResult的响应,它是通过XML-RPC调用传递给XMLRPC服务器的ab参数的总和。

我使用的是PHP XML-RPC 3.0.0beta(我也可以使用2.2.2,但我决定使用3.0.0beta,因为它被标记为稳定)。

我拿到了。

这是一个完整的、最简单的基于phpxmlrpc库的XML-RPC服务器。

<?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
                    )          
            )
        );
?>

我不完全理解scalarval()方法对参数的作用,我认为它只是将对象转换为正则变量/值,可以与字符串一起使用。