XML-RPC for PHP Unable to add params in a single struct

1k Views Asked by At

I am using XML-RPC Lib for PHP to do an XML-RPC request to an external server

The method requires the input to be in the following format:

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

I am following the examples provided in the library and I used the following code to generate the request. The code I tried is:

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

but the request that gets sent out looks like below (with each param wrapped inside a 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>

How do I change my code to send the request in the desired format shown above? I used "array" instead of struct while creating the PhpXmlRpc\Value in the foreach loop but still I didn't get the desired format. I also tried to use the addStruct method available in the PhpXmlRpc\Value class but the request is sent empty. Any ideas?

1

There are 1 best solutions below

0
On

Here is the correct code:

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

And this is the generated payload:

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