downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Wstęp> <SoapVar::SoapVar
Last updated: Fri, 23 Jul 2010

view this page in

XML-RPC



add a note add a note User Contributed Notes
XML-RPC
rion
27-Jul-2010 12:53
Here is also my wrapper with some magic inside =)
It has interface very similar to python's xmlrpclib.
see example in the bottom of post

<?php
class PrettyXMLRPCFault
{
    const
ConnectionError = 8002;
   
    public
$code = 0;
    public
$string = "";
   
    public function
__construct($code, $string)
    {
       
$this->code = $code;
       
$this->string = $string;
    }
   
    public function
__toString()
    {
        return
"XML-RPC Fault<".$this->code.": ".$this->string.">";
    }
}

class
PrettyXMLRPCCrumb
{
    public
$parent = null;
    protected
$name = "";
   
    public function
__construct($parent, $name)
    {
       
$this->parent = $parent;
       
$this->name = $name;
    }
   
    public function
__get($name)
    {
        return new
PrettyXMLRPCCrumb($this, $name);
    }
   
    public function
__call($name, $arguments)
    {
       
$this->parent->__call($this->name.".".$name, $arguments);
    }
}

class
PrettyXMLRPC extends PrettyXMLRPCCrumb
{
    public
$url = "";
   
    public function
__construct($url, $backend) {
       
$this->url = $url;
       
$this->backend = $backend;
       
$this->backend->setUrl($url);
    }
   
    public function
__call($name, $arguments) {
        echo
$this->backend->call($name, $arguments);
    }
}

class
PrettyXMLRPCEpiBackend
{
    private
$server;
    private
$url;
    private
$user;
    private
$password;
   
    public function
__construct()
    {
       
$this->server = xmlrpc_server_create();
    }
   
    public function
__destruct()
    {
       
xmlrpc_server_destroy($this->server);
    }
   
    public function
setUrl($url)
    {
       
$url = parse_url($url);
       
$scheme = array_key_exists("scheme", $url) ? $url["scheme"] : "http";
       
$this->user = array_key_exists("user", $url) ? $url["user"] : "";
       
$this->password = array_key_exists("pass", $url) ? $url["pass"] : "";
       
$port = array_key_exists("port", $url) ? $url["port"]
                                    : (
$this->scheme == 'https'?443:80);
       
       
$this->url = $scheme."://".$url["host"].":".$port
           
.(array_key_exists("path", $url) ? $url["path"] : "")
            .(
array_key_exists("query", $url) ? '?'.$url["query"] : "")
            .(
array_key_exists("fragment", $url) ? "#".$url["fragment"] : "");
    }
   
    public function
call($name, $arguments)
    {
       
$request = xmlrpc_encode_request($name, $arguments);
       
$headers = array("Content-Type: text/xml");
        if (
$this->user) {
           
array_push($headers, "Authorization: Basic "
                   
.base64_encode($this->user.":".$this->password));
        }
       
       
$header = (version_compare(phpversion(), '5.2.8')) > 0
           
? $headers : implode("\r\n", $headers);
           
       
// allow_self_signed is for my own use. remove it for more safety
       
$context = stream_context_create(array('http' => array(
           
'method' => "POST",
           
'header' => $header,
           
'content' => $request,
           
'user_agent' => 'PrettyXMLRPC',
           
'allow_self_signed' => true
       
)));
       
$file = @file_get_contents($this->url, false, $context);
        if (
$file === false) {
           
$le = error_get_last();
            return new
PrettyXMLRPCFault(PrettyXMLRPCFault::ConnectionError, $le["message"]);
        } else {
           
$response = xmlrpc_decode($file);
            if (
is_array($response) && xmlrpc_is_fault($response)) {
                return new
PrettyXMLRPCFault($response["faultCode"], $response["faultString"]);
            } else {
                return
$response;
            }
        }
    }
}

$r = new PrettyXMLRPC("https://guest:guest@127.0.0.1:1234", new PrettyXMLRPCEpiBackend);
$r->people->programmers->php->notifyAll("This message is sent by PrettyXMLRPC php class.");
?>
aim at secoya dot dk
15-Sep-2009 03: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;
    }
}
?>

Wstęp> <SoapVar::SoapVar
Last updated: Fri, 23 Jul 2010
 
 
show source | credits | sitemap | contact | advertising | mirror sites