如何解析我在PHP中作为soap API响应获得的XML响应中的值

时间:2014-11-29 05:01:42

标签: php xml parsing soap

我得到了一个XML响应

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.*********.in">
   <SOAP-ENV:Body>
      <ns1:LoginResponse>
         <return>
            <SessionID>3AC46E3F62</SessionID>
            <ResponseCode>0</ResponseCode>
            <ResponseMessage>Successful</ResponseMessage>
         </return>
      </ns1:LoginResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如何使用PHP解析此xml中的SessionID和ResponseCode.Im无法执行此操作。请提供一些方法来执行此操作。

1 个答案:

答案 0 :(得分:1)

选中此项:How can I get and process a response from a PHP API并应用print_r(htmlentities($ xmldata-&gt; asXML()));到您的XML响应

这也是:soap:Envelope SOAP-ENV:Envelope PHP

那里的回复可能会对你有所帮助,当然你可能会编码自己的原因非常容易

检查此代码也已经在运行示例(非常接近您的问题):

<?php

$a ='<soap-env:envelope xmlns:ns1="http://v3.core.com.productserve.com/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <soap-env:body>
    <ns1:getproductlistresponse>
      <oproduct>
        <iid>113133802</iid>
        <icategoryid>270</icategoryid>
        <imerchantid>1547</imerchantid>
        <iadult>0</iadult>
        <sname>The Ashes / 5th Test - England v Australia - Day 1</sname>
        <sawdeeplink>http://www.awin1.com/pclick.php?p=113133802&amp;a=111402&amp;m=1547&amp;platform=cs</sawdeeplink>
        <sawthumburl>http://images.productserve.com/thumb/1547/113133802.jpg</sawthumburl>
        <fprice>119.99</fprice>
      </oproduct>
    </ns1:getproductlistresponse>
  </soap-env:body>
</soap-env:envelope>';



$xml = simplexml_load_string($a);


$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['soap-env']);
$getproductlistresponse = $soap->body->children($ns['ns1']);
foreach ($getproductlistresponse->children() as $item)
{
  //This example just accesses the iid node but the others are all available.
  echo (string) $item->iid . '<br />';
}

?>