处理SOAP响应

时间:2012-11-30 17:08:47

标签: php soap

我正在尝试从First Data的全局网关处理SOAP响应。我之前使用过SoapClient,但没有wsdl - 该公司表示他们不提供。

我已经尝试了各种其他方法,例如基于此处和PHP手册中的示例的SimpleXMLElement,但我无法获得任何工作。我怀疑命名空间是我的问题的一部分。任何人都可以建议一种方法或指向一个类似的例子 - 我迄今为止谷歌的努力没有结果。

使用PHP 5.

部分SOAP响应(在它被剥离之前的所有HTML标题内容)看起来像这样:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

<SOAP-ENV:Header/>

<SOAP-ENV:Body>

<fdggwsapi:FDGGWSApiOrderResponse xmlns:fdggwsapi="http://secure.linkpt.net/fdggwsapi/schemas_us/fdggwsapi">

<fdggwsapi:CommercialServiceProvider/>

<fdggwsapi:TransactionTime>Thu Nov 29 17:03:18 2012</fdggwsapi:TransactionTime>

<fdggwsapi:TransactionID/>

<fdggwsapi:ProcessorReferenceNumber/>

<fdggwsapi:ProcessorResponseMessage/>

<fdggwsapi:ErrorMessage>SGS-005005: Duplicate transaction.</fdggwsapi:ErrorMessage>

<fdggwsapi:OrderId>A-e833606a-5197-45d6-b990-81e52df41274</fdggwsapi:OrderId>
...

<snip>

我还需要能够确定是否发出了SOAP错误信号。 XML的内容如下所示:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<SOAP-ENV:FaultX>
<faultcode>SOAP-ENV:Client</faultcode>
<faultstring xml:lang="en">MerchantException</faultstring>
<detail>
cvc-pattern-valid: Value '9999185.00' is not facet-valid with respect to pattern '([1-9]([0-9]{0,3}))?[0-9](\.[0-9]{1,2})?' for type '#AnonType_ChargeTotalAmount'.
cvc-type.3.1.3: The value '9999185.00' of element 'v1:ChargeTotal' is not valid.
</detail>
</SOAP-ENV:FaultX>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

使用Code先生的答案,我能够从非故障响应中检索数据。但是我需要确定我正在处理什么类型的数据包并从这两种类型中提取数据。只要他们提供wsdl就会容易得多!

1 个答案:

答案 0 :(得分:6)

你的回答可以用SimpleXML解析,这是一个例子。请注意,我将命名空间URL传递给children()以访问元素。

$obj = simplexml_load_string($xml);

$response = $obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://secure.linkpt.net/fdggwsapi/schemas_us/fdggwsapi')->FDGGWSApiOrderResponse;

echo $response->TransactionTime . "\n";
echo $response->ErrorMessage;

<强>输出

  

Thu Nov 29 17:03:18 2012
  SGS-005005:重复交易。

<强> Codepad Demo

编辑:SoapFault响应可以解析如下。它输出故障字符串和详细信息,或“未发现故障”:

if($obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://schemas.xmlsoap.org/soap/envelope/') && isset($obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://schemas.xmlsoap.org/soap/envelope/')->children()->faultcode))
{
    $fault = $obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://schemas.xmlsoap.org/soap/envelope/')->children();

    // soap fault
    echo $fault->faultstring;
    echo $fault->detail;
}
else
{
    echo 'No fault found, do normal parsing...';
}
相关问题