如何在PHP

时间:2016-02-12 10:07:06

标签: php soap wsdl soap-client soapfault

我正在调用第三方Web服务,以通过提供的凭据获取用户详细信息。以下是我的肥皂客户请求。

$client = new \SoapClient($url, array("exception" => 0));        
$auth = '
<wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>' . $userName . '</wsse:Username>
<wsse:Password>' . $password . '</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>';
$authvalues = new \SoapVar($auth, XSD_ANYXML);
$header1 = new \SoapHeader("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", $authvalues, true);   
$header2 = new \SoapHeader("http://xmlns.myweb.com/FulfillProductRequest/V1", "v1");    
$header3 = new \SoapHeader("http://xmlns.myweb.com/ParameterType/V2", "v2");    
$header4 = new \SoapHeader("http://xmlns.myweb.com/RequestHeader/V3", "v3");

$header = array();    
$header[] = $header1;    
$header[] = $header2;    
$header[] = $header3;    
$header[] = $header4;                
$client->__setSoapHeaders($header);    
$res = $client->FulfillProduct($FulfillProductRequest);    

当我调用WSDL时,如果我得到成功响应,则没有问题。

但如果soapFault中出现任何错误,那么我的代码会显示致命错误。

任何人都知道如何处理SoapFault plz共享。

感谢先生。

1 个答案:

答案 0 :(得分:0)

您可以尝试将$res = $client→FulfillProduct($FulfillProductRequest);包裹在try { } catch() {}

例如:

try { 
  $res = $client→FulfillProduct($FulfillProductRequest);
} catch(Exception $e) {
  // An error has occured.
  // All information about the error has been stored in variable $e
  print_r($e);
}

请注意,您可以将SoapClient设置为永远不会在设置中引发异常,因此请先查看。