PHP SoapClient:如何将SOAP参数标记名称添加到命名空间?

Gre*_*g K 2 php soap soap-client

我正在使用PHP SoapClient来使用SOAP服务,但是收到的错误是SOAP服务无法看到我的参数.

<tns:GenericSearchResponse xmlns:tns="http://.../1.0">
  <tns:Status>
    <tns:StatusCode>1</tns:StatusCode>
    <tns:StatusMessage>Invalid calling system</tns:StatusMessage>
  </tns:Status>
</tns:GenericSearchResponse>
Run Code Online (Sandbox Code Playgroud)

XML PHP的SoapClient为SOAP调用发送:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns1="http://.../1.0">
  <SOAP-ENV:Body>
    <ns1:GenericSearchRequest>
      <UniqueIdentifier>12345678</UniqueIdentifier>
      <CallingSystem>WEB</CallingSystem>
    </ns1:GenericSearchRequest>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)

我最初使用soap-ui,在使用相同的WSDL时成功运行.XML soap-ui发送呼叫:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://.../1.0">
  <SOAP-ENV:Body>
    <ns1:GenericSearchRequest>
      <ns1:UniqueIdentifier>12345678</ns1:UniqueIdentifier>
      <ns1:CallingSystem>WEB</ns1:CallingSystem>
    </ns1:GenericSearchRequest>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)

不同的是UniqueIdentifierCallingSystem参数ns1在soap-ui请求中作为前缀.

我已尝试将传递SoapVar对象用于SoapClient调用,但这不会增加参数标记并为其添加前缀ns1.

我知道这WEB是一个有效的CallingSystem值,因为XSD指定它,并且它在使用soap-ui时有效.

我目前的SoapClient代码:

try {
  $client = new SoapClient($wsdl, array('trace' => 1));
  $query = new stdClass;
  $query->UniqueIdentifier = $id;
  $query->CallingSystem = 'WEB';
  $response = $client->GenericUniqueIdentifierSearch($query);
} catch (SoapFault $ex) {
  $this->view->error = $ex->getMessage();
  ...
}
Run Code Online (Sandbox Code Playgroud)

我找到了这篇博文,但我希望可能会有更清晰的实施.

更新: 使用此问题解决方案但非常笨重:

$xml = "<ns1:GenericSearchRequest>"
     . "<ns1:UniqueIdentifier>$id</ns1:UniqueIdentifier>"
     . "<ns1:CallingSystem>WEB</ns1:CallingSystem>"
     . "</ns1:GenericSearchRequest>";
$query = new SoapVar($xml, XSD_ANYXML);

$response = $this->client->__SoapCall(
    'GenericUniqueIdentifierSearch',
    array($query)
);
Run Code Online (Sandbox Code Playgroud)

小智 6

我发现这样做的合理方法是使用SoapVar和SoapParam的组合.

注意,SoapVar具有指定每个var的命名空间的选项.

所以你的代码应该是这样的:

$wrapper = new StdClass;
$wrapper->UniqueIdentifier = new SoapVar($id, XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema", "UniqueIdentifier", "ns1");
$wrapper->CallingSystem = new SoapVar("WEB", XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema", "CallingSystem", "ns1");
$searchrequest = new SoapParam($wrapper, "GenericSearchRequest");

try{
    $response = $this->client->GenericUniqueIdentifierSearch($searchrequest);
}catch(Exception $e){
    die("Error calling method: ".$e->getMessage());
}
Run Code Online (Sandbox Code Playgroud)

如果您遇到属性和方法获得不同命名空间的问题,请尝试将SoapVar的命名空间指定为信封中定义的URL(在您的示例中为" http://.../1.0 "),如:

$wrapper->UniqueIdentifier = new SoapVar($id, XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema", "UniqueIdentifier", "http://.../1.0");
Run Code Online (Sandbox Code Playgroud)

有关所有XSD_*常量的列表,请参阅Soap常量.


Gre*_*g K 5

使用SoapVarGenericSearchRequest字段命名:

$xml = "<ns1:GenericSearchRequest>"
     . "<ns1:UniqueIdentifier>$id</ns1:UniqueIdentifier>"
     . "<ns1:CallingSystem>WEB</ns1:CallingSystem>"
     . "</ns1:GenericSearchRequest>";
$query = new SoapVar($xml, XSD_ANYXML);

$response = $this->client->__SoapCall(
    'GenericUniqueIdentifierSearch',
    array($query)
);
Run Code Online (Sandbox Code Playgroud)