很遗憾,我没有任何 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);
这很好用,我可以通过以下方式检索类型和函数:
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
函数。
我得到了这个与 SoapUi 配合良好的 XML 字符串程序:
<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或 this other project这可能很有用(搜索更多 PHP WS-Addressing
很容易)。同样,我只做过研究,没有任何实践经验来帮助您编写实际代码:)
关于php - 如何使用 SoapClient 将参数传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33167125/