我有一个正在发送的肥皂请求,它可以生成有效的响应,或者如果请求有问题,它会返回错误字符串。
以下是有效的回复
<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;
}
但是当响应出现错误时,代码不会打印错误字符串。