【问题标题】:Parsing SOAP response from server解析来自服务器的 SOAP 响应
【发布时间】:2013-09-01 07:52:48
【问题描述】:

任何人都可以帮助我解析肥皂响应。我尝试了很多很多方法,但这对我没有帮助。我卡住了。 有回应:

  <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 soap 练习,我没有很多知识。 谁能帮帮我?

【问题讨论】:

标签: php xml web-services soap


【解决方案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);

希望对你有所帮助。

【讨论】:

  • 感谢您的快速回答,但 print_r($array, true);什么都不返回 :( 试过 var_dump($array); 它返回 NULL。
  • 您使用的函数来自恶意软件站点。我已经删除了它。此外,您给出的建议只是意味着您不知道 XML 命名空间是什么。您的回答实际上阻碍了@user2736875 可以学到一些东西。相反,你已经表现出不好的做法。尝试找出 XML 命名空间的工作原理以及 PHP 必须提供哪些 XML 解析器。根本没有理由将其转换为数组。
  • 如果你这样做是因为你很懒,那么请只链接到一个现有的答案,该答案显示了 OP 正在寻找什么。
  • @hakre 好吧,第一次感谢您的信息。我不是 PHP 专家,如果它对我有用,它可以为正在寻找快速答案的人工作。直到现在没有人回答。当然,更感谢像您这样的大师以您的经验回答。如果您发布正确答案,我会给您 +1。谢谢。顺便说一句,我是从我的网站上拿的,没有用谷歌搜索
  • 别担心,这是重复的,我已经投票反对。
猜你喜欢
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多