Web Services
在线手册:中文 英文
PHP手册

XML-RPC


Web Services
在线手册:中文 英文
PHP手册
PHP手册 - N: XML-RPC

用户评论:

aim at secoya dot dk (15-Sep-2009 04:08)

Here is a little wrapper class I just developed. It simply calls the methods you register with it.

<?php
class XMLRPCServer {
   
    private
$serverHandler;
   
    private
$externalFunctions;
   
    public function
__construct() {
       
$this->serverHandler = xmlrpc_server_create();
       
$this->externalFunctions = array();
    }
   
    public function
registerMethod($externalName, $function, $parameterNames) {
        if(
$function == null) $function = $externalName;
       
xmlrpc_server_register_method($this->serverHandler, $externalName, array(&$this, 'callMethod'));
       
$this->externalFunctions[$externalName] =
            array(
'function'       => $function,
                 
'parameterNames' => $parameterNames);
    }
   
    public function
callMethod($functionName, $parametersFromRequest) {
       
$function = $this->externalFunctions[$functionName]['function'];
       
$parameterNames = $this->externalFunctions[$functionName]['parameterNames'];
       
$parameters = array();
        foreach(
$parameterNames as $parameterName) {
           
$parameters[] = $parametersFromRequest[0][$parameterName];
        }
        return
call_user_func_array($function, $parameters);
    }
   
    public function
computeAnswer() {
        return
xmlrpc_server_call_method($this->serverHandler, file_get_contents('php://input'), null);
    }
}

// USAGE EXAMPLE HERE
$xmlRPCServer = new XMLRPCServer();
$someServer = new SomeXmlRPCServer($xmlRPCServer);
$answer = $xmlRPCServer->computeAnswer();
header('Content-Type: text/xml');
print(
$answer);
class
SomeXmlRPCServer{
   
    private
$xmlRPCServer;
   
    public function
__construct($xmlRPCServer) {
       
$this->xmlRPCServer = $xmlRPCServer;
       
$this->xmlRPCServer->registerMethod(
           
'selectDatabase',        // The name the XML-RPC Client calls
           
array(&$this, 'selectDatabaseInternal'),    // Pointer to the method, can be a simple string if you have global functions
           
array('dbName', 'something')        // Name of the parameters and their ordering
       
);
    }
   
    public function
selectDatabaseInternal($dbName, $test) {
        return
'dbName:.'.$dbName.'.test:'.$test;
    }
}
?>