Question

Need a tutorial or some instruction on how to use the XML-RPC library built in to PHP (version PHP Version 5.2.6) for a XML-RPC client. The server is in Python and works.

Google and php.net are failing me.

Update:

Per phpinfo I have xmlrpc-epi v. 0.51 installed. I visited http://xmlrpc-epi.sourceforge.net/ but the xmlrpc-epi-php examples section on the left showed me sf.net's version of a 404.

Update2:

I'm going to use http://phpxmlrpc.sourceforge.net/ and hopefully that will work out for me.

Update3:

The code at http://phpxmlrpc.sourceforge.net/ was straightforward and I got working.

Not closing the question. If anyone wants to chime in with ultra-simple solutions, that would be great!

Was it helpful?

Solution

A very simple xmlrpc client, I use a cURL class, you can get it from: https://github.com/dcai/curl/blob/master/src/dcai/curl.php

class xmlrpc_client {
    private $url;
    function __construct($url, $autoload=true) {
        $this->url = $url;
        $this->connection = new curl;
        $this->methods = array();
        if ($autoload) {
            $resp = $this->call('system.listMethods', null);
            $this->methods = $resp;
        }
    }
    public function call($method, $params = null) {
        $post = xmlrpc_encode_request($method, $params);
        return xmlrpc_decode($this->connection->post($this->url, $post));
    }
}
header('Content-Type: text/plain');
$rpc = "http://10.0.0.10/api.php";
$client = new xmlrpc_client($rpc, true);
$resp = $client->call('methodname', array());
print_r($resp);

OTHER TIPS

Looking for the same solution. This is a super simple class that can theoretically work with any XMLRPC server. I whipped it up in 20 minutes, so there is still a lot to be desired such as introspection, some better error handling, etc.

file: xmlrpcclient.class.php

<?php

/**
 * XMLRPC Client
 *
 * Provides flexible API to interactive with XMLRPC service. This does _not_
 * restrict the developer in which calls it can send to the server. It also
 * provides no introspection (as of yet).
 *
 * Example Usage:
 *
 * include("xmlrpcclient.class.php");
 * $client = new XMLRPCClient("http://my.server.com/XMLRPC");
 * print var_export($client->myRpcMethod(0));
 * $client->close();
 *
 * Prints:
 * >>> array (
 * >>>   'message' => 'RPC method myRpcMethod invoked.',
 * >>>   'success' => true,
 * >>> )
 */

class XMLRPCClient
{
    public function __construct($uri)
    {
        $this->uri = $uri;
        $this->curl_hdl = null;
    }

    public function __destruct()
    {
        $this->close();
    }

    public function close()
    {
        if ($this->curl_hdl !== null)
        {
            curl_close($this->curl_hdl);
        }
        $this->curl_hdl = null;
    }

    public function setUri($uri)
    {
        $this->uri = $uri;
        $this->close();
    }

    public function __call($method, $params)
    {
        $xml = xmlrpc_encode_request($method, $params);

        if ($this->curl_hdl === null)
        {
            // Create cURL resource
            $this->curl_hdl = curl_init();

            // Configure options
            curl_setopt($this->curl_hdl, CURLOPT_URL, $this->uri);
            curl_setopt($this->curl_hdl, CURLOPT_HEADER, 0); 
            curl_setopt($this->curl_hdl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($this->curl_hdl, CURLOPT_POST, true);
        }

        curl_setopt($this->curl_hdl, CURLOPT_POSTFIELDS, $xml);

        // Invoke RPC command
        $response = curl_exec($this->curl_hdl);

        $result = xmlrpc_decode_request($response, $method);

        return $result;
    }
}

?>

I've written a simple Object Oriented wrapper which makes it as easy as:

    require_once('ripcord.php');
    $client = ripcord::xmlrpcClient( $url );
    $score  = $client->method( $argument, $argument2, .. );

See http://code.google.com/p/ripcord/wiki/RipcordClientManual for more information and a download link.

I found this solution in http://code.runnable.com/UnEjkT04_CBwAAB4/how-to-create-a-xmlrpc-server-and-a-xmlrpc-client-for-php

Example for login in webfaction api

// login is the method in the xml-rpc server and username and password
// are the params
$request = xmlrpc_encode_request("login", array('username', 'password'));

$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
'content' => $request
)));

$server = 'https://api.webfaction.com/'; // api url
$file = file_get_contents($server, false, $context);

$response = xmlrpc_decode($file);

print_r($response);

You will see something like:

Array ( [0] => 5d354f42dcc5651fxe6d1a21b74cd [1] => Array ( [username] => yourusername [home] => /home [mail_server] => Mailbox14 [web_server] => Webxxx [id] => 123456 ) )

Wordpress has XML-RPC.php file take a look at that.. it might help

Additionally, fxmlrpc (when used with NativeSerializer and NativeParser) is a thin wrapper around ext/xmlrpc.

From the php official link http://www.php.net/manual/en/ref.xmlrpc.php use steph 's example (at the bottom) as a starting point. He is using the same server and its easy to set up.That is if you do not wish to use the external library or framework. But if you do then have a look at http://framework.zend.com/manual/1.12/en/zend.xmlrpc.server.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top