2017-05-15 6 views
1

Ich habe diesen Code:PHP Parsing eine 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>"; 

Dies ist die Antwort:

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

) 

frage ich mich, warum ich eine leere Antwort bin immer, weil das nicht sein sollte der Fall, weil ich versuchte, es in boomerang und postman zu tun, erhalte ich Daten.

Dies ist die Antwort von Postbote oder Bumerang:

<?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> 

Gibt es ein Problem mit meinem Code, den ich leer Körper Ergebnisse bin immer?

Ihre Hilfe wird geschätzt. Vielen Dank.

+0

Können Sie Antwortstring und erwartete Ausgabe teilen? –

+0

@SahilGulati Ich habe meinen Beitrag aktualisiert und eine Beispielantwort gezeigt. – PinoyStackOverflower

+1

Sie können DOMDocument für diese verwenden, können Sie mir sagen, was Sie extrahieren möchten, damit ich Ihnen helfen kann .. –

Antwort

1

Hier verwende ich DOMDocument, um Seife Antwort zu parsen.

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); 

Ausgang:

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

Sie sind die Besten, danke !! – PinoyStackOverflower

+0

@PinoyStackOverflower: D: D willkommen ... nicht mehr als du .... :) –