我在探索使用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中?
谢谢马里奥!这正是我需要的。我对PEAR软件包没有太多的经验,并且在他们的文档中有点失落。我标记你的答案是正确的,如果它允许我的话,你会赞成。 – 2012-01-17 20:39:00
嗯,确实是这样,PEAR文件并不总是可用的,并不是所有情况下都有用。而且这个类也只是比较简单的PHP内置函数早一些。 (不要担心投票,如果你能报告回答哪个答案有效就足够了,这对未来的访问者最有帮助。) – mario 2012-01-17 21:34:04