2017-05-15 194 views
1

我有這樣的代碼:PHP解析SOAP XML

$xml = '<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:ServiceQualification"> 
    <x:Header> 
     <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
      <wsse:UsernameToken> 
       <wsse:Username>'.$username.'</wsse:Username> 
       <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$password.'</wsse:Password> 
      </wsse:UsernameToken> 
     </wsse:Security> 
    </x:Header> 
    <x:Body> 
     <urn:AddressSearch> 
      <urn:property> 
       <urn:unit_no>'.$unit_no.'</urn:unit_no> 
       <urn:house_no>'.$house_no.'</urn:house_no> 
       <urn:lot_no>'.$lot_no.'</urn:lot_no> 
       <urn:street_name>'.$street_name.'</urn:street_name> 
       <urn:street_type>'.$street_type.'</urn:street_type> 
       <urn:suburb>'.$suburb.'</urn:suburb> 
       <urn:state_name>'.$state_name.'</urn:state_name> 
       <urn:postcode>'.$postcode.'</urn:postcode> 
      </urn:property> 
     </urn:AddressSearch> 
    </x:Body> 
</x:Envelope>'; 

$response = curl($url,$xml); 

$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response); 

$xml_response = simplexml_load_string($clean_xml) or die("Error: Cannot create object"); 
echo "<pre>"; 
print_r($xml_response); 
echo "</pre>"; 

這是響應:

SimpleXMLElement Object 
(
    [Body] => SimpleXMLElement Object 
     (
     ) 

) 

我很納悶,爲什麼我得到一個空白的響應,因爲這不應該是這種情況,因爲我試圖在boomerangpostman這樣做,我正在獲取數據。

這是從郵遞員或自食其果響應:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:ServiceQualification"> 
    <SOAP-ENV:Body> 
     <ns1:AddressSearchResponse xmlns:ns1="urn:ServiceQualification"> 
      <return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:ServiceQualificationResponse[1]"> 
       <item xsi:type="tns:ServiceQualificationResponse"> 
        <status_code xsi:type="xsd:string">1</status_code> 
        <property_id xsi:type="xsd:string">253323</property_id> 
        <unit_no xsi:type="xsd:string">Basement</unit_no> 
        <house_no xsi:type="xsd:string">8</house_no> 
        <lot_no xsi:type="xsd:string">3</lot_no> 
        <street_name xsi:type="xsd:string">Harbour</street_name> 
        <street_type xsi:type="xsd:string">Road</street_type> 
        <suburb xsi:type="xsd:string">Hamilton</suburb> 
        <state_name xsi:type="xsd:string">QLD</state_name> 
        <postcode xsi:type="xsd:string">4007</postcode> 
        <estate_name xsi:type="xsd:string">Hamilton Harbour</estate_name> 
        <stage xsi:type="xsd:string"></stage> 
        <property_class xsi:type="xsd:string">Class 3</property_class> 
       </item> 
      </return> 
     </ns1:AddressSearchResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

是否有我的代碼,我漸漸空虛了身體的結果有問題嗎?

您的幫助表示讚賞。謝謝。

+0

你能分享響應字符串和預期產出? –

+0

@SahilGulati我更新了我的帖子並顯示了一個示例回覆。 – PinoyStackOverflower

+1

你可以使用DOMDocument,你能告訴我你想提取什麼,這樣我就可以幫你解決問題。 –

回答

1

在這裏,我使用DOMDocument來解析肥皂響應。

Try this code snippet here

<?php 
ini_set('display_errors', 1); 
$string='<?xml version="1.0" encoding="ISO-8859-1"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:ServiceQualification"> 
    <SOAP-ENV:Body> 
     <ns1:AddressSearchResponse xmlns:ns1="urn:ServiceQualification"> 
      <return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:ServiceQualificationResponse[1]"> 
       <item xsi:type="tns:ServiceQualificationResponse"> 
        <status_code xsi:type="xsd:string">1</status_code> 
        <property_id xsi:type="xsd:string">253323</property_id> 
        <unit_no xsi:type="xsd:string">Basement</unit_no> 
        <house_no xsi:type="xsd:string">8</house_no> 
        <lot_no xsi:type="xsd:string">3</lot_no> 
        <street_name xsi:type="xsd:string">Harbour</street_name> 
        <street_type xsi:type="xsd:string">Road</street_type> 
        <suburb xsi:type="xsd:string">Hamilton</suburb> 
        <state_name xsi:type="xsd:string">QLD</state_name> 
        <postcode xsi:type="xsd:string">4007</postcode> 
        <estate_name xsi:type="xsd:string">Hamilton Harbour</estate_name> 
        <stage xsi:type="xsd:string"></stage> 
        <property_class xsi:type="xsd:string">Class 3</property_class> 
       </item> 
      </return> 
     </ns1:AddressSearchResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope>'; 

$domDocument = new DOMDocument(); 
$domDocument->loadXML($string); 
$carriers=array(); 
$results=$domDocument->getElementsByTagName("item"); 
foreach($results as $result) 
{ 
    foreach($result->childNodes as $node) 
    { 
     if($node instanceof DOMElement) 
     { 
      array_push($carriers, $node->textContent); 
     } 
    } 

} 
print_r($carriers); 

輸出:

Array 
(
    [0] => 1 
    [1] => 253323 
    [2] => Basement 
    [3] => 8 
    [4] => 3 
    [5] => Harbour 
    [6] => Road 
    [7] => Hamilton 
    [8] => QLD 
    [9] => 4007 
    [10] => Hamilton Harbour 
    [11] => 
    [12] => Class 3 
) 
+0

你是最棒的,謝謝! – PinoyStackOverflower

+0

@PinoyStackOverflower:D:D歡迎...不只是你.... :) –