2012-11-30 145 views
5

我正在嘗試處理來自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就會容易得多!

回答

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; 

輸出

週四11月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...'; 
} 
+0

感謝 - 這正是我所需要的 - 我還沒有看到其中兩個命名空間中一樣,引用的例子。 – JonP

+0

如果我得到一個有效的回答,該示例運行得非常好,但當發出肥皂故障時,我發現了一個額外的「皺紋」。在那種情況下,當然沒有第二個命名空間,我不能確定一個確定是否存在故障元素及其內容的簡單方法。你能提出什麼建議嗎? – JonP

+0

用肥皂故障響應的例子更新這個問題,並讓我們看看。 – MrCode