需要PHP5的示例XML-RPC客户端代码

时间:2009-04-05 05:11:33

标签: php xml-rpc

需要有关如何使用PHP内置的XML-RPC库(版本为PHP V5.2.6)的教程或一些指令来处理XML-RPC客户端。服务器是用Python工作的。

谷歌和php.net让我失望。

更新

每个phpinfo我安装了 xmlrpc-epi v。0.51 。我访问了http://xmlrpc-epi.sourceforge.net/,但左边的xmlrpc-epi-php示例部分向我展示了sf.net的404版本。

UPDATE2:

我将使用http://phpxmlrpc.sourceforge.net/,希望这对我有用。

UPDATE3:

http://phpxmlrpc.sourceforge.net/的代码很简单,我开始工作了。

没有结束这个问题。如果有人想要使用超简单的解决方案,那就太棒了!

8 个答案:

答案 0 :(得分:13)

一个非常简单的xmlrpc客户端,我使用cURL类,你可以从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);

答案 1 :(得分:4)

寻找相同的解决方案。这是一个超级简单的类,理论上可以与任何XMLRPC服务器一起使用。我在20分钟内把它掀起,所以还有很多不足之处,比如内省,更好的错误处理等等。

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

?>

答案 2 :(得分:3)

我编写了一个简单的面向对象的包装器,它使它变得如此简单:

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

有关详细信息和下载链接,请参阅 http://code.google.com/p/ripcord/wiki/RipcordClientManual

答案 3 :(得分:3)

我在http://code.runnable.com/UnEjkT04_CBwAAB4/how-to-create-a-xmlrpc-server-and-a-xmlrpc-client-for-php

中找到了这个解决方案

在webfaction api中登录的示例

scrollview.setScrollY

你会看到类似的东西:

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

答案 4 :(得分:1)

Wordpress有XML-RPC.php文件看一下..它可能有帮助

答案 5 :(得分:0)

此外,fxmlrpc(与NativeSerializerNativeParser一起使用时)是ext/xmlrpc周围的薄包装。

答案 6 :(得分:0)

从php官方链接http://www.php.net/manual/en/ref.xmlrpc.php使用steph的例子(在底部)作为起点。他正在使用相同的服务器并且易于设置。即如果您不希望使用外部库或框架。但如果你这样做,那么看看http://framework.zend.com/manual/1.12/en/zend.xmlrpc.server.html

答案 7 :(得分:0)

用php编写的客户端和服务器:

https://tldp.org/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-php.html

相关问题