PHP的XML-RPC无法在单个结构中添加params

时间:2016-12-30 14:07:45

标签: php xml struct xml-rpc

我正在使用XML-RPC Lib来向外部服务器发送XML-RPC请求

该方法要求输入采用以下格式:

<?xml version="1.0"?>
<methodCall>
<methodName>GetAllowedService</methodName>
<params>
<param>
<value>
<struct>
<member>
<name>NodeType</name>
<value>
<string>A</string>
</value>
</member>
<member>
<name>originHostName</name>
<value>
<string>Admin1</string>
</value>
</member>
<member>
<name>originTransactionID</name>
<value>
<string>566613</string>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodCall>

我正在关注库中提供的示例,并使用以下代码生成请求。我试过的代码是:

$inAr = array("NodeType" => "A", "originHostName" => "Admin1", "originTransactionID" => "566613");
print "This is the input data:<br/><pre>";
foreach($inAr as $key => $val) {
    print $key . ", " . $val . "\n";
}
print "</pre>";

// create parameters from the input array: an xmlrpc array of xmlrpc structs
$p = array();
foreach ($inAr as $key => $val) {
    $p[] = new PhpXmlRpc\Value(
        array(
            $key => new PhpXmlRpc\Value($val)
        )
    ,"struct"
    );
}
$v = new PhpXmlRpc\Value($p, "struct");
// create client and message objects
$req = new PhpXmlRpc\Request('GetAllowedService', $v);
$client = new PhpXmlRpc\Client("http://serverip");
$client->setCredentials('user','pass');

// set maximum debug level, to have the complete communication printed to screen
$client->setDebug(2);

// send request
print "Now sending request (detailed debug info follows)";
$resp = $client->send($req);

但是发出的请求如下所示(每个param包含在一个struct中):

<?xml version="1.0"?>
<methodCall>
<methodName>GetAllowedService</methodName>
<params>
<param>
<value><struct>
<member><name>originNodeType</name>
<value><string>A</string></value>
</member>
</struct></value>
</param>
<param>
<value><struct>
<member><name>originHostName</name>
<value><string>Admin1</string></value>
</member>
</struct></value>
</param>
<param>
<value><struct>
<member><name>originTransactionID</name>
<value><string>566613</string></value>
</member>
</struct></value>
</param>
<param>
<value><struct>
</struct></value>
</param>
</params>
</methodCall>

如何更改我的代码以上面显示的所需格式发送请求?我使用&#34; array&#34;在foreach循环中创建PhpXmlRpc \ Value时,而不是struct,但我仍然没有得到所需的格式。我还尝试使用PhpXmlRpc \ Value类中提供的addStruct方法,但请求被发送为空。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这是正确的代码:

event_stream
+ id BIGINT PRIMARY AUTO-INCREMENT
+ event TEXT

这是生成的有效载荷:

$inAr = array("NodeType" => "A", "originHostName" => "Admin1", "originTransactionID" => "566613");
print "This is the input data:<br/><pre>";
foreach($inAr as $key => $val) {
    print $key . ", " . $val . "\n";
}
print "</pre>";

// create parameters from the input array: an xmlrpc array of xmlrpc structs
$p = array();
foreach ($inAr as $key => $val) {
    $p[$key] = new PhpXmlRpc\Value($val);
}
$v = new PhpXmlRpc\Value($p, "struct");

// create client and message objects
$req = new PhpXmlRpc\Request('GetAllowedService', array($v));
$client = new PhpXmlRpc\Client("http://serverip");
$client->setCredentials('user','pass');

// set maximum debug level, to have the complete communication printed to screen
$client->setDebug(2);

// send request
print "Now sending request (detailed debug info follows)";
$resp = $client->send($req);