Php 解析来自服务器的SOAP响应

Php 解析来自服务器的SOAP响应,php,xml,web-services,soap,Php,Xml,Web Services,Soap,有人能帮我解析soap响应吗。我尝试了很多方法,但那对我没有帮助。我被绊倒了。 有人回应: <soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:body> <ns2:findflightresponse xmlns:ns2="http://cxf.apache.org"> <return> <cls

有人能帮我解析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文件。这是我第一次使用xmlsoap,我还没有掌握很多知识。 有人能帮我吗?

你可以这样试试:

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

希望它能对您有所帮助。

谢谢您的快速回答,但请打印\r$array,真的;不返回任何内容:尝试使用var_dump$array;它返回NULL。您使用的函数来自恶意软件站点。我把它拿走了。另外,您给出的建议只是意味着您不知道什么是XML名称空间。你的答案实际上阻碍了@user2736875学习一些东西。相反,你表现出了坏习惯。尝试了解XML名称空间是如何工作的,以及PHP必须提供哪些XML解析器。根本没有理由将其转换为数组。如果您这样做是因为懒惰,那么请链接到一个现有的答案,显示OP正在寻找的内容。@hakre好的,首先感谢您提供的信息。我不是PHP专家,如果它对我有效,它可以为寻找快速答案的人工作。直到现在还没有人回答。当然,像你这样的大师以你的经验给出的答案更值得赞赏。如果你给出正确答案,我会给你+1。谢谢顺便说一句,我是从我的网站上拿的,没有用谷歌搜索。不用担心,这是一个副本,我已经投票反对了。可能的副本