php 需要 PHP5 的示例 XML-RPC 客户端代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/718377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need sample XML-RPC client code for PHP5
提问by Christopher Mahan
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.
需要有关如何将内置于 PHP(PHP 版本 5.2.6)的 XML-RPC 库用于 XML-RPC 客户端的教程或一些说明。服务器使用 Python 编写并且可以正常工作。
Google and php.net are failing me.
谷歌和 php.net 让我失望。
Update:
更新:
Per phpinfo I have xmlrpc-epi v. 0.51installed. 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.
根据 phpinfo,我安装了xmlrpc-epi v. 0.51。我访问了http://xmlrpc-epi.sourceforge.net/但左侧的 xmlrpc-epi-php 示例部分向我展示了 sf.net 的 404 版本。
Update2:
更新2:
I'm going to use http://phpxmlrpc.sourceforge.net/and hopefully that will work out for me.
我将使用http://phpxmlrpc.sourceforge.net/,希望这对我有用。
Update3:
更新3:
The code at http://phpxmlrpc.sourceforge.net/was straightforward and I got working.
http://phpxmlrpc.sourceforge.net/ 上的代码很简单,我开始工作了。
Not closing the question. If anyone wants to chime in with ultra-simple solutions, that would be great!
不结束问题。如果有人想加入超简单的解决方案,那就太好了!
回答by Dongsheng Cai
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
一个非常简单的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);
回答by PeqNP
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.
寻找相同的解决方案。这是一个超级简单的类,理论上可以与任何 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;
}
}
?>
回答by herohat
I found this solution in http://code.runnable.com/UnEjkT04_CBwAAB4/how-to-create-a-xmlrpc-server-and-a-xmlrpc-client-for-php
我在http://code.runnable.com/UnEjkT04_CBwAAB4/how-to-create-a-xmlrpc-server-and-a-xmlrpc-client-for-php 中找到了这个解决方案
Example for login in webfaction api
登录 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 ) )
回答by Auke
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/RipcordClientManualfor more information and a download link.
看 http://code.google.com/p/ripcord/wiki/RipcordClientManual了解更多信息和下载链接。
回答by dswatik
Wordpress has XML-RPC.php file take a look at that.. it might help
Wordpress 有 XML-RPC.php 文件看看那个..它可能有帮助
回答by Lars Strojny
回答by kta
From the php official link http://www.php.net/manual/en/ref.xmlrpc.phpuse 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
从 php 官方链接http://www.php.net/manual/en/ref.xmlrpc.php使用 steph 的示例(在底部)作为起点。他正在使用相同的服务器,并且易于设置。也就是说,如果您不想使用外部库或框架。但是,如果您这样做,请查看http://framework.zend.com/manual/1.12/en/zend.xmlrpc.server.html

