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

时间:2017-09-11 03:54:29

标签: php object soap soap-client

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

<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>

如何使用object-way创建有效的XML?

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

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

它可以让您轻松完成请求构建和响应处理。

相关问题