Need sample XML-RPC client code for PHP5

Christopher Mahan picture Christopher Mahan · Apr 5, 2009 · Viewed 43k times · Source

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!

Answer

Dongsheng Cai picture Dongsheng Cai · Apr 5, 2009

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);