如何使用 SoapClient 将参数传递给函数?

How to pass a parameter to a function with SoapClient?

很遗憾,我没有任何 SOAP 经验,请原谅我最终在下面使用了错误的符号 -

我得到了 2 个 URL 和一个 XML 字符串,我正在尝试从远程 SOAP 服务器获得 XML 响应。

第一个 URL 是一个 WSDL 文档,我将它提供给 SoapClient 构造函数:

$client = new SoapClient(URL_1, array('trace' => 1, 'exceptions' => 0));

第二个URL是SOAP服务器,我这里用的是:

$client->__setLocation(URL_2);

这很好用,我能够检索 typesfunctions with:

print_r( $client->__getTypes() );
print_r( $client->__getFunctions() );

这是输出中有趣的部分:

[28] => struct GetServiceInfo {
 UserAuthentification UserAuthentification;
}

[74] => struct UserAuthentification {
 string UserId;
 string Password;
 string SessionKey;
}

[3] => GetServiceInfoResponse GetServiceInfo(GetServiceInfo $parameters)

我的问题是调用上面的GetServiceInfo函数.

我得到了这个 XML 字符串,它适用于 SoapUi 程序:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://...../V1">
  <soap:Header>.....</soap:Header>   
  <soap:Body>
      <v1:GetServiceInfo>
         <UserAuthentification>
            <UserId>MY_USERNAME</UserId>
            <Password>MY_PASSWORD</Password>
          </UserAuthentification>
      </v1:GetServiceInfo>
   </soap:Body>
</soap:Envelope>

请问如何在我的 PHP 脚本中传递这个参数?

我试过:

$result = $client->GetServiceInfo(array(
        new SoapParam( "UserAuthentification", array(   # ERROR, WANTS STRING HERE
        new SoapParam( "UserId", "MY_USERNAME" ),
        new SoapParam( "Password", "MY_PASSWORD" ),
)))); 

var_dump( $client->__getLastRequest() );
var_dump( $client->__getLastResponse() );

print_r( $result );

但是得到错误:

Warning: SoapParam::SoapParam() expects parameter 2 to be string, array given in soap.php

Fatal error: SOAP-ERROR: Encoding: object has no 'UserAuthentification' property in soap.php

我想我在这里漏掉了一些小东西,请帮忙。

更新:

我通过以下步骤更进一步:

$user = array('UserId' => 'MY_USERNAME', 'Password' => 'MY_PASSWORD');
$params = array('UserAuthentification' => $user);
$result = $client->GetServiceInfo($params);

现在我得到错误:

[FaultDescription] => Unable to identify service

[FaultReason] => wsa:To is missing and the given URL /services could not be mapped to an existing service

服务器使用 WS-Addressing(由 FaultReason 证明 - 关键字是 wsa:To),常规 PHP SoapClient 扩展不支持。

我还没有使用过 WS-Addressing,但我最近正在分析一个 API 我们将来要处理的项目需要它。

请注意 this project or this other project 这可能会有用(很容易搜索到更多 PHP WS-Addressing)。同样,我只做过研究,没有任何实践经验来帮助您编写实际代码:)