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

标签 php xml soap soap-client

很遗憾,我没有任何 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 projectthis other project这可能很有用(搜索更多 PHP WS-Addressing 很容易)。同样,我只做过研究,没有任何实践经验来帮助您编写实际代码:)

关于php - 如何使用 SoapClient 将参数传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33167125/

相关文章:

php - symfony2 - 如何在每个页面中全局检索数据库中的数据

PHP 扩展命名空间库

java - Imageview 不采用卡片 View 的角半径

java - 类型不匹配 : cannot convert from ViewGroup. LayoutParams 到 LinearLayout.LayoutParams

java - war 中的文件路径

php - simplexml_load_string() 不会读取标签中带有 "soap:"的 soap 响应

javascript - ionic 3 HTTP-GET 连接输入用户名和密码以访问 php 页面

PHP/Javascript : Restart Form Button Isn't Working, 内部代码

带有命名空间条目数组的 PHP SoapHeader

java - 使用 Java 添加 Soap Action header