首页 > 解决方案 > 如何在考虑肥皂故障响应的同时解析 php 响应

问题描述

我有一个正在发送的soap请求,它可以生成一个有效的响应,或者如果请求有问题,它会返回一个故障字符串。

以下是有效回复

<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
    <ns2:importTransactionResponse
        xmlns:ns2="http://xxxxx/">
        <return>                
            <userId>434079</userId>
        </return>
    </ns2:importTransactionResponse>
</soapenv:Body>

然后下面是具有故障字符串的响应。

<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
    <soapenv:Fault>
        <faultcode>soapenv:Server</faultcode>
        <faultstring>Errors occured when validating the userId of the 
 reuest.
userId errors.invalid</faultstring>
        <detail>
            <ns2:WebServiceValidationException
                xmlns:ns2="http://xx">
                <message>Errors occured when validating the userId of the 
reuest.
userId errors.invalid</message>
                <validationErrors>userId errors.invalid</validationErrors>
            </ns2:WebServiceValidationException>
        </detail>
    </soapenv:Fault>
</soapenv:Body>

下面是我用来解析这个响应的代码。

try {


  $response = curl_exec($cURL);


    if (curl_errno($curl)) {
    echo 'Curl Error: ' . curl_error($curl) . "\n\n\n\n";


    }
  $doc = new DOMDocument();
  $doc->loadXML($response);

  $userId= $doc->getElementsByTagName('userId')->item(0)->nodeValue;
  echo userId;

 } 
 catch (SoapFault $fault){
 echo $fault->faultstring;
 }

但是当响应有错误时,代码不会打印错误字符串。

标签: phpcurlsoap

解决方案


推荐阅读