PHP 具有复杂类型的 Soap 客户端

PHP Soap client with complex types

我正在尝试使用此结构获取请求:

<SOAP-ENV:Body>
  <ns1:getCreditReportTypes>
    <reportTypeRequest>
      <reportParams xsi:type="ns1:personCreditReportParams">
        <personId>4</personId>
        <consentConfirmed>true</consentConfirmed>
      </reportParams>
    </reportTypeRequest>
  </ns1:getCreditReportTypes>
</SOAP-ENV:Body>

这是我的 php-代码:

$obj = new \stdClass();
$obj->personId = 4;
$obj->consentConfirmed = true;
$data = new \SoapVar($obj, SOAP_ENC_OBJECT, "personCreditReportParams", $namespace, "reportParams");
$res = $this->client->getCreditReportTypes(new \SoapParam($data,"reportTypeRequest"));

然而,php生成无效的xml:

<SOAP-ENV:Body>
  <ns1:getCreditReportTypes xsi:type="ns1:personCreditReportParams">
    <consentConfirmed>true</consentConfirmed>
    <personId>4</personId>
  </ns1:getCreditReportTypes>
</SOAP-ENV:Body>

如何使用对象方式创建有效的 XML?

您应该明确地使用 WSDL 到 php 生成器,例如 PackageGenerator

它将简化请求构造和响应处理。

对于那些会遇到同样问题的人。 我的解决方案是使用 nusoap (https://github.com/yaim/nusoap-php7)。该库允许您发出复杂的请求,包括 SWA(带附件的 SOAP)。 这是我的问题的工作代码:

$person = array("personId"=>$id, "consentConfirmed"=>$confirmed);
$data = array(
  "reportParams"=>new soapval("reportParams", "personCreditReportParams", $person, false, $namespace)
);
$result = $client->call("getCreditReportTypes", $data, $namespace);

P.S。我已经尝试了一些生成器,但没有人可以发出正确的请求,尽管 类 已正确生成。