2016-12-30 49 views
0

我使用XML-RPC Lib爲PHP做一個XML-RPC請求到外部服務器XML-RPC的PHP無法在單個結構添加PARAMS

的方法要求輸入要在以下格式:

<?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包裹結構內):

<?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> 

如何更改我的代碼發送請求以上面顯示的所需格式?我在foreach循環中創建PhpXmlRpc \ Value時使用「數組」而不是結構,但仍然沒有獲得所需的格式。我也嘗試使用PhpXmlRpc \ Value類中可用的addStruct方法,但請求被髮送爲空。有任何想法嗎?

回答

0

下面是正確的代碼:

$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); 

這是生成的有效載荷:

---SENDING--- 
POST/HTTP/1.0 
User-Agent: XML-RPC for PHP 4.3.0 
Host: serverip 
Authorization: Basic dXNlcjpwYXNz 
Accept-Encoding: gzip, deflate 
Accept-Charset: UTF-8,ISO-8859-1,US-ASCII 
Content-Type: text/xml 
Content-Length: 410 

<?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> 
---END---