XML-RPC
- Введение
- Installing/Configuring
- Предопределенные константы
- XML-RPC Функции
- xmlrpc_decode_request — Decodes XML into native PHP types
- xmlrpc_decode — Decodes XML into native PHP types
- xmlrpc_encode_request — Generates XML for a method request
- xmlrpc_encode — Generates XML for a PHP value
- xmlrpc_get_type — Gets xmlrpc type for a PHP value
- xmlrpc_is_fault — Determines if an array value represents an XMLRPC fault
- xmlrpc_parse_method_descriptions — Decodes XML into a list of method descriptions
- xmlrpc_server_add_introspection_data — Adds introspection documentation
- xmlrpc_server_call_method — Parses XML requests and call methods
- xmlrpc_server_create — Creates an xmlrpc server
- xmlrpc_server_destroy — Destroys server resources
- xmlrpc_server_register_introspection_callback — Register a PHP function to generate documentation
- xmlrpc_server_register_method — Register a PHP function to resource method matching method_name
- xmlrpc_set_type — Sets xmlrpc type, base64 or datetime, for a PHP string value
Коментарии
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;
}
}
?>