处理SOAP响应
问题描述:
我正在尝试处理来自First Data的全局网关的SOAP响应。我以前使用过SoapClient,但没有wsdl - 公司表示他们不提供。处理SOAP响应
我已经尝试了各种其他方法,如基于这里和PHP手册中找到的例子的SimpleXMLElement,但我无法获得任何工作。我怀疑命名空间是我的问题的一部分。任何人都可以提出一种方法,或者将我指向一个类似的例子 - 我的Google努力迄今为止没有结果。
使用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>
使用代码先生的答案我已经能够检索来自非故障响应的数据。但是我需要确定我正在处理哪种类型的数据包,并从两种类型中提取数据。只要他们提供wsdl就会容易得多!
答
您的回复可以用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;
输出
周四11月29日17时03分18秒2012
SGS-005005:复制交易。
编辑:响应的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...';
}
感谢 - 这正是我所需要的 - 我还没有看到其中两个命名空间中一样,引用的例子。 – JonP
如果我得到一个有效的回答,该示例运行得非常好,但当发出肥皂故障时,我发现了一个额外的“皱纹”。在那种情况下,当然没有第二个命名空间,我不能确定一个确定是否存在故障元素及其内容的简单方法。你能提出什么建议吗? – JonP
用肥皂故障响应的例子更新这个问题,并让我们看看。 – MrCode