2012-01-17 47 views
0

我在探索使用XMLRPC服务在Drupal系统和我们网站的另一部分之间进行通信。我发现了PHP的php_xmlrpc扩展的一些示例代码,但发现我们的Web主机不支持该扩展。PEAR XML_RPC_encode vs PHP xmlrpc_encode_request

取而代之的是,他们所提供的PEAR XML_RPC包

它出现的两种方法进行编码非常不同。

我用来设置该请求的PHP代码,基于http://drupal.org/node/339845

$method_name = 'user.login'; 
$user_credentials = array(
    0 => 'example.user', 
    1 => 'password', 
); 

// any user-defined arguments for this service 
// here we use the login credentials we specified at the top of the script 
$user_args = $user_credentials; 
$required_args=array(); 

// add the arguments to the request 
foreach ($user_args as $arg) { 
    array_push($required_args, $arg); 
} 

...then call the XMLRPC functions here... 

我在我的电脑上测试php_xmlrpc与WAMPserver,并从php_xmlrpc功能xmlrpc_encode_request(http://us.php.net/manual/en/function.xmlrpc-encode-request.php)返回我所需要的,是这样的:

<?xml version="1.0" encoding="iso-8859-1"?> 
<methodCall> 
<methodName>user.login</methodName> 
<params> 
<param> 
    <value> 
    <string>example.user</string> 
    </value> 
</param> 
<param> 
    <value> 
    <string>password</string> 
    </value> 
</param> 
</params> 
</methodCall> 

而PEAR XML_RPC_encode()函数返回此:

Array 
(
    [0] => example.user 
    [1] => password 
) 
object(XML_RPC_Value)#1 (2) { 
    ["me"]=> 
    array(1) { 
    ["string"]=> 
    string(10) "user.login" 
    } 
    ["mytype"]=> 
    int(1) 
} 

PEAR XML_RPC中是否有另外一个函数可以将参数编码到XML中?

回答

1

该文档可从http://pear.php.net/manual/en/package.webservices.xml-rpc.api.php

为了让XML编组输出首先构造一个消息,而不是,然后使用->serialize()方法:

$msg = new XML_RPC_Message("function", array(new XML_RPC_Value(123, "int"))); 
print $msg->serialize(); 

XML_RPC_encode()功能是为了换一个普通的php数组变成这样的XML_RPC_*对象。

但是很显然,PEAR类主要是通过处理原始数据转换的XML_RPC_Client接口使用。

+0

谢谢马里奥!这正是我需要的。我对PEAR软件包没有太多的经验,并且在他们的文档中有点失落。我标记你的答案是正确的,如果它允许我的话,你会赞成。 – 2012-01-17 20:39:00

+0

嗯,确实是这样,PEAR文件并不总是可用的,并不是所有情况下都有用。而且这个类也只是比较简单的PHP内置函数早一些。 (不要担心投票,如果你能报告回答哪个答案有效就足够了,这对未来的访问者最有帮助。) – mario 2012-01-17 21:34:04