duanbi3151
2011-08-29 16:50
浏览 87
已采纳

NuSOAP - 函数参数未正确映射

I'm writing a web service using NuSOAP in PHP. To test it, I have written both a server and a client. On the server end, I register the function like so:

$server->register(
    "testFunction",
    array("param1" => "xsd:string", "param2" => "xsd:string"),
    array("result" => "xsd:string"),
    "http://localhost/testApp"
);

And on the client looks like this:

require_once("./lib/nusoap.php");

$client = new soapclient("http://localhost/testApp/server.php");
$function = $_GET["function"];
unset($_GET["function"]);
$result = $client->call($function, $_GET);

echo "<pre>". print_r($result, true) ."</pre>";

When I call it like

http://localhost/testApp/client.php?function=testFunction&param1=value1&param2=value2

it works fine, but if I switch param1 and param2 and say

http://localhost/testApp/client.php?function=testFunction&param2=value2&param1=value1

then param1 gets the value value2 and param2 gets the value value1. So obviously, it just goes by the order of the parameters, not the names.

I figured that since I registered the function with specific parameter names and then called the function, specifying those parameter names, that they would be assigned accordingly.

Am I missing something? What's the point of specifying the parameter names if they will just be thrown out and assigned in whatever order you entered them? Is there a way to make it so that I can enter the parameters in any order and have them map correctly?

图片转代码服务由CSDN问答提供 功能建议

我正在使用PHP中的NuSOAP编写Web服务。 为了测试它,我写了一个服务器和一个客户端。 在服务器端,我注册了这样的函数:

  $ server-&gt; register(
“testFunction”,
 array(“param1”=&gt;“  xsd:string“,”param2“=&gt;”xsd:string“),
 array(”result“=&gt;”xsd:string“),
”http:// localhost / testApp“
);  
   
 
 

在客户端上看起来像这样:

  require_once(“./ lib / nusoap.php”  ); 
 
 $ client = new soapclient(“http://localhost/testApp/server.php”); 
 $ function = $ _GET [“function”]; 
unset($ _ GET [“function”]  ); 
 $ result = $ client-&gt; call($ function,$ _GET); 
 
echo“&lt; pre&gt;”。  print_r($ result,true)。“&lt; / pre&gt;”; 
   
 
 

当我称之为

 < 代码> http://localhost/testApp/client.php?function = testFunction&amp; param1 = value1&amp; param2 = value2 
   
 
 

它工作正常,但如果我切换 param1和param2并说

  http://localhost/testApp/client.php?function = testFunction&amp; param2 = value2&amp; param1 = value1 
   
 
 

然后param1获取值 value2 ,param2获取值 value1 。 显然,它只是按参数的顺序而不是名称。

我认为既然我用特定的参数名称注册了函数然后调用了函数,那么指定那些参数名称 ,他们将被相应地分配。

我错过了什么吗? 如果参数名称只是被抛出并以您输入的顺序分配,那么指定参数名称有什么意义呢? 有没有办法让它能以任何顺序输入参数并让它们正确映射?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douchen2025 2011-09-20 15:32
    已采纳

    maybe go for a default map in the client:

        $default = array(
            'param1' => '',
            'param2' => 'test',
        );
        $params = $defaults;
        foreach ( $_GET as $param_name => $param_value ) {
            if ( isset( $params[ $param_name ] ) ) $params[$param_name] = $param_value;
        }
        $client->call( $function, $params );
    

    Simple hack, even though it doesnt "really" answer your questions.

    One note, the url you're passing to the client MUST be a SOAP WSDL if you expect the client to know anything about the format of the calls being made. Is yours ? ( It's been a long time I've used nuSoap, but i recall simply adding "?wsdl" param to the server url was enough to have the server respond with a valid wsdl )

    点赞 打赏 评论

相关推荐 更多相似问题