从服务器解析SOAP响应

时间:2013-09-01 07:52:48

标签: php xml web-services soap

任何人都可以帮我解析肥皂反应。我尝试了很多方法,但那并没有帮助我。我被困了。 有答复:

  <soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:body>
    <ns2:findflightresponse xmlns:ns2="http://cxf.apache.org">
      <return>
        <clstobook>K</clstobook>
        <flightsegment>
          <arrivalairport>
            <locationcode>LHR</locationcode>
            <terminal>1</terminal>
          </arrivalairport>
          <arrivaldatetime>2013-09-01T16:15:00</arrivaldatetime>
          <bookingclassavaillist>
            <resbookdesigcode>W</resbookdesigcode>
            <resbookdesigquantity>0</resbookdesigquantity>
            <resbookdesigstatuscode>3</resbookdesigstatuscode>
            <rph>1</rph>
          </bookingclassavaillist>
          <bookingclassavaillist>
            <resbookdesigcode>U</resbookdesigcode>
            <resbookdesigquantity>0</resbookdesigquantity>
            <resbookdesigstatuscode>2</resbookdesigstatuscode>
            <rph>1</rph>
          </bookingclassavaillist>
          <departureairport>
            <locationcode>VKO</locationcode>
          </departureairport>
          <departuredatetime>2013-09-01T15:10:00</departuredatetime>
          <equipment>
            <airequiptype>738</airequiptype>
          </equipment>
          <flightnumber>353</flightnumber>
          <marketingairline>
            <code>UN</code>
          </marketingairline>
          <marketingcabin>
            <meal>B</meal>
          </marketingcabin>
          <rph>1</rph>
          <stopquantity>0</stopquantity>
        </flightsegment>
        <lateforsale>false</lateforsale>
        <noseats>false</noseats>
      </return>
    </ns2:findflightresponse>
  </soap:body>
</soap:envelope>

我尝试过:

$xml_response = $ch_result;
$xml = simplexml_load_string($xml_response,NULL,NULL,"http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('ns2', 'http://cxf.apache.org');
print_r($xml);

但是,它返回:

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

我从不使用xml文件。这是我第一次用xml肥皂练习,而且我没有很多知识。 有人可以帮助我吗?

1 个答案:

答案 0 :(得分:-1)

您可以尝试这种方式:

        $xml = str_replace("soap:", "", $xml);
        $xml = str_replace("ns2:", "", $xml);
        $xml = str_replace("ns3:", "", $xml);
        $xml = str_replace("&#0", "", $xml);

        $array = xml2array($xml);
        $array = $array['envelope']['body']['findflightresponse'];

        print_r($array, true);

希望它会对你有所帮助。