dpd3982 2016-12-30 14:07
浏览 41

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

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 . "
";
}
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条回答 默认 最新

  • douci2516 2017-11-24 00:59
    关注

    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 . "
    ";
    }
    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---
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题