【问题标题】:XML-RPC server implementation in PHP - minimal, simplest possiblePHP 中的 XML-RPC 服务器实现 - 最小,最简单的可能
【发布时间】:2013-02-19 15:49:17
【问题描述】:

我正在尝试用 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 服务器的 a 和 b 参数的总和。

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

【问题讨论】:

    标签: php xml-rpc


    【解决方案1】:

    我明白了。

    这是基于 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() 方法对参数的作用,我认为它只是将对象转换为常规变量/值,并且可以与字符串一起使用。

    【讨论】:

      猜你喜欢
      • 2016-05-19
      • 2011-07-27
      • 2011-02-11
      • 2020-06-16
      • 2011-12-17
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多