10

需要有关如何为 XML-RPC 客户端使用 PHP(版本 PHP 版本 5.2.6)内置的 XML-RPC 库的教程或说明。服务器在 Python 中并且可以工作。

谷歌和 php.net 让我失望了。

更新:

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

更新2:

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

更新3:

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

不关闭问题。如果有人想加入超简单的解决方案,那就太好了!

4

8 回答 8

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);
于 2009-04-05T07:13:01.173 回答
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;
    }
}

?>
于 2011-05-12T19:08:28.477 回答
3

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

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

http://code.google.com/p/ripcord/wiki/RipcordClientManual了解更多信息和下载链接。

于 2010-08-12T11:38:50.783 回答
3

我在http://code.runnable.com/UnEjkT04_CBwAAB4/how-to-create-a-xmlrpc-server-and-a-xmlrpc-client-for-php找到了这个解决方案

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

你会看到类似的东西:

Array ( [0] => 5d354f42dcc5651fxe6d1a21b74cd [1] => Array ( [username] => yourusername [home] => /home [mail_server] => Mailbox14 [web_server] => Webxxx [id] => 123456 ) )
于 2015-08-11T22:00:41.597 回答
1

Wordpress 有 XML-RPC.php 文件,看看那个.. 它可能会有所帮助

于 2009-04-05T06:08:55.273 回答
0

此外,fxmlrpc(与NativeSerializerand一起使用时NativeParser)是ext/xmlrpc.

于 2013-03-30T00:38:46.113 回答
0

从 php 官方链接http://www.php.net/manual/en/ref.xmlrpc.php使用 steph 的示例(在底部)作为起点。他使用的是同一台服务器,并且易于设置。也就是说,如果您不想使用外部库或框架。但是,如果您这样做,请查看http://framework.zend.com/manual/1.12/en/zend.xmlrpc.server.html

于 2013-04-12T11:59:46.523 回答
0

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

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

于 2020-11-02T15:18:38.867 回答