PHP-Soap 传递方法参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/810054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:55:28  来源:igfitidea点击:

PHP-Soap pass method parameters

phpsoap

提问by Fabian

I have relatively no experience with SOAP. Im trying to work with a webservice for a client using WSDL mode. I'm having trouble passing parameters with the method and getting them to come the parameters to show up in the request as needed. I'm using the standard php soap class.

我对 SOAP 相对没有经验。我正在尝试使用 WSDL 模式为客户端使用 Web 服务。我在使用该方法传递参数并根据需要让它们成为显示在请求中的参数时遇到了问题。我正在使用标准的 php 肥皂类。

I need my SOAP request to be structured like the following:

我需要像下面这样构造我的 SOAP 请求:

 <?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://hostserver/">
    <SOAP-ENV:Body>
        <ns1:DoLogin>
            <ns1:request>
                <ns1:Session>
                    <ns1:SessionId>00000000-0000-0000-0000-000000000000</ns1:SessionId>
                </ns1:Session>
                <ns1:UserCredential>
                    <ns1:UserName>username</ns1:UserName>
                    <ns1:Password>password</ns1:Password>
                    <ns1:ApplicationID>00000000-0000-0000-0000-000000000000</ns1:ApplicationID>
                    <ns1:ClientID>00000000-0000-0000-0000-000000000000</ns1:ClientID>
                    <ns1:ClientVersion>V1.0</ns1:ClientVersion>
                </ns1:UserCredential>
            </ns1:request>
        </ns1:DoLogin>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

In php I call the function like so:

在 php 中,我这样调用函数:

$client->DoLogin($args);

And the request ends up like this:

请求最终是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://hostserver/"><SOAP-ENV:Body><ns1:DoLogin/></SOAP-ENV:Body></SOAP-ENV:Envelope>

No matter how I pass the args (single varabiables, array, class object) I cant get the request to have the structure as that.

无论我如何传递参数(单个变量、数组、类对象),我都无法获得具有该结构的请求。

Can anyone help me? I'm sure its going to be something quite simple.

谁能帮我?我相信它会很简单。

回答by Ben Gribaudo

While working on a slightly related problem today, I found that the following PHP produced the SOAP request shown below:

今天在处理一个稍微相关的问题时,我发现以下 PHP 产生了如下所示的 SOAP 请求:

$sc = new SoapClient($url);
$params = array('step' => 'ShippingInfo', 'value' => "hello");
$result = $sc->__soapCall('runStep', array('parameters' => $params));
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
 <SOAP-ENV:Body>
  <ns1:runStep>
     <ns1:step>ShippingInfo</ns1:step>
     <ns1:value>hello</ns1:value>
  </ns1:runStep>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

One catch I found was that if the parameters ($params) I passed did not conform to what was specified in the WSDL file, the message that the SOAP client generated would look similar to what you complain of--a message with a body contains no data value. I wonder if your problem lies here.

我发现的一个问题是,如果我传递的参数 ($params) 不符合 WSDL 文件中指定的内容,则 SOAP 客户端生成的消息将与您抱怨的类似——带有正文的消息包含没有数据价值。我想知道你的问题是否出在这里。

Also, note how the above PHP uses two arrays to pass parameters. The first array contains the parameters and their names. The second contains the first array. Interesting syntax, I know. :-)

另外,请注意上面的 PHP 如何使用两个数组来传递参数。第一个数组包含参数及其名称。第二个包含第一个数组。有趣的语法,我知道。:-)

FYI, the example code above being used to communicate with a C# .Net WCF service having the following contract:

仅供参考,上面的示例代码用于与具有以下合同的 C# .Net WCF 服务进行通信:

[OperationContract]
string runStep(string step, string value);

回答by bgondy

try this:

尝试这个:

$soapClient->__soapCall('methodToCall', array('parameters' => $yourParamsArray));

That worked for me with a .NET webservice.

这对我有用 .NET web 服务。

回答by knsmith

This is valid way: right mr.bgondy
$result = $soapClient->methodsomefunction("params1"=>"value","params2"=>"value2");

my question is that you said

我的问题是你说

$soapClient->__soapCall('methodToCall', array('parameters' => $yourParamsArray));

above code for .net web services but when i have tried this method for .net asp web services, parameters values not parse i think so because when i removed params=value not effect same "Unknown error occured!" from .net asp web services in iframe.

上面的 .net Web 服务代码,但是当我为 .net asp Web 服务尝试这种方法时,参数值无法解析我认为是因为当我删除 params=value 时效果不一样“发生未知错误!” 来自 iframe 中的 .net asp web 服务。

回答by Face666

I had the same problem.

我有同样的问题。

This one solved for me:

这个为我解决了:

$result = $soapClient->somefunction(array( "param1" => "value1", "param2" => "value2" ));

$result = $soapClient->somefunction(array( "param1" => "value1", "param2" => "value2" ));

Parameter types (and order) must be the same as what the current wsdl defines. (string, object, etc)

参数类型(和顺序)必须与当前 wsdl 定义的相同。(字符串、对象等)

回答by Posto

try this,

尝试这个,

$client->__soapCall('methodName',array('requestObj'=>$requestObj));