2013-02-19 69 views
2

我試圖在PHP中編寫簡單的XMLRPC服務器。PHP中的XML-RPC服務器實現 - 儘可能簡單,最簡單

我讀過一些文檔,我發現最小的實現,與此類似:

// /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,這是ab參數通過XMLRPC調用傳遞到XMLRPC服務器總和。

即時通訊使用PHP XML-RPC 3.0.0beta(我也可以使用2.2.2,但我決定使用3.0.0beta,因爲它標記爲穩定)。

回答

4

我明白了。

這是基於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()用做參數,我認爲它只是轉換對象常規變量/值,它可能與字符串中使用。