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

发布于 2024-07-16 14:29:24 字数 1431 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

ぃ弥猫深巷。 2024-07-23 14:29:24

一个非常简单的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);

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);
陌路黄昏 2024-07-23 14:29:24

寻找相同的解决方案。 这是一个超级简单的类,理论上可以与任何 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;
    }
}

?>

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

?>
微凉徒眸意 2024-07-23 14:29:24

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

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

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

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.

日暮斜阳 2024-07-23 14:29:24

我在 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 ) )

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 ) )
番薯 2024-07-23 14:29:24

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

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

半夏半凉 2024-07-23 14:29:24

此外,fxmlrpc(与NativeSerializerNativeParser) 是 ext/xmlrpc 的薄包装。

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

仅此而已 2024-07-23 14:29:24

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文