沃梦达 / 编程问答 / php问题 / 正文

如何在没有 SoapClient 的情况下解析 SOAP 响应

How to parse SOAP response without SoapClient(如何在没有 SoapClient 的情况下解析 SOAP 响应)

本文介绍了如何在没有 SoapClient 的情况下解析 SOAP 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几天我一直在尝试解析 SOAP 响应,但我无法让它工作.我希望能够获得所有oproduct"对象.

I have spent the last few days trying to parse a SOAP response but I can't get it to work. I would like to be able to get all the "oproduct" objects.

我这样做而不是使用内置的 SOAP 客户端,因为我需要使用 multi_curl 执行并行请求.

I am doing it this way instead of using the built in SOAP client because I need to do parallel requests using multi_curl.

这是我的代码:

$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($xml->xpath('//oproduct') as $item)
{
  // do something
}

回复如下:

<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>
      <oproduct>
        <iid>113133791</iid>
        <icategoryid>270</icategoryid>
        <imerchantid>1547</imerchantid>
        <iadult>0</iadult>
        <sname>The Ashes / 1st Test - England v Australia - Day 1</sname>
        <sawdeeplink>http://www.awin1.com/pclick.php?p=113133791&amp;a=111402&amp;m=1547&amp;platform=cs</sawdeeplink>
        <sawthumburl>http://images.productserve.com/thumb/1547/113133791.jpg</sawthumburl>
        <fprice>69.99</fprice>
      </oproduct>
      <oproduct>
        <iid>51155349</iid>
        <icategoryid>428</icategoryid>
        <imerchantid>513</imerchantid>
        <iadult>0</iadult>
        <sname>Rapitest DTM 118 Damp Test Meter</sname>
        <sawdeeplink>http://www.awin1.com/pclick.php?p=51155349&amp;a=111402&amp;m=513&amp;platform=cs</sawdeeplink>
        <sawthumburl>http://images.productserve.com/thumb/513/51155349.jpg</sawthumburl>
        <fprice>18.35</fprice>
      </oproduct>
      <oproduct>
        <iid>187948</iid>
        <icategoryid>474</icategoryid>
        <imerchantid>496</imerchantid>
        <iadult>0</iadult>
        <sname>Rapitest Dm 10 Electrical Multimeter</sname>
        <sawdeeplink>http://www.awin1.com/pclick.php?p=187948&amp;a=111402&amp;m=496&amp;platform=cs</sawdeeplink>
        <sawthumburl>http://images.productserve.com/thumb/496/187948.jpg</sawthumburl>
        <fprice>26.71</fprice>
      </oproduct>
      <oproduct>
        <iid>51155353</iid>
        <icategoryid>428</icategoryid>
        <imerchantid>513</imerchantid>
        <iadult>0</iadult>
        <sname>Rapitest DM 10 Electrical Multimeter</sname>
        <sawdeeplink>http://www.awin1.com/pclick.php?p=51155353&amp;a=111402&amp;m=513&amp;platform=cs</sawdeeplink>
        <sawthumburl>http://images.productserve.com/thumb/513/51155353.jpg</sawthumburl>
        <fprice>22.7</fprice>
      </oproduct>
      <oproduct>
        <iid>49152850</iid>
        <icategoryid>474</icategoryid>
        <imerchantid>1366</imerchantid>
        <iadult>0</iadult>
        <sname>Testboy Profi LED Plus</sname>
        <sawdeeplink>http://www.awin1.com/pclick.php?p=49152850&amp;a=111402&amp;m=1366&amp;platform=cs</sawdeeplink>
        <sawthumburl>http://images.productserve.com/thumb/1366/49152850.jpg</sawthumburl>
        <fprice>44.99</fprice>
      </oproduct>
      <oproduct>
        <iid>51155350</iid>
        <icategoryid>428</icategoryid>
        <imerchantid>513</imerchantid>
        <iadult>0</iadult>
        <sname>Rapitest ETP 116 Electrical Test Probe</sname>
        <sawdeeplink>http://www.awin1.com/pclick.php?p=51155350&amp;a=111402&amp;m=513&amp;platform=cs</sawdeeplink>
        <sawthumburl>http://images.productserve.com/thumb/513/51155350.jpg</sawthumburl>
        <fprice>17.55</fprice>
      </oproduct>
      <oproduct>
        <iid>51155351</iid>
        <icategoryid>428</icategoryid>
        <imerchantid>513</imerchantid>
        <iadult>0</iadult>
        <sname>Rapitest ET 111 Electrical Test Meter</sname>
        <sawdeeplink>http://www.awin1.com/pclick.php?p=51155351&amp;a=111402&amp;m=513&amp;platform=cs</sawdeeplink>
        <sawthumburl>http://images.productserve.com/thumb/513/51155351.jpg</sawthumburl>
        <fprice>13.55</fprice>
      </oproduct>
      <oproduct>
        <iid>72326941</iid>
        <icategoryid>474</icategoryid>
        <imerchantid>496</imerchantid>
        <iadult>0</iadult>
        <sname>Rapitest Etp 116 Electrical Test Probe</sname>
        <sawdeeplink>http://www.awin1.com/pclick.php?p=72326941&amp;a=111402&amp;m=496&amp;platform=cs</sawdeeplink>
        <sawthumburl>http://images.productserve.com/thumb/496/72326941.jpg</sawthumburl>
        <fprice>20.66</fprice>
      </oproduct>
      <oproduct>
        <iid>49338340</iid>
        <icategoryid>206</icategoryid>
        <imerchantid>80</imerchantid>
        <iadult>0</iadult>
        <sname>Ladies Protest Zekke Jacket True Black</sname>
        <sawdeeplink>http://www.awin1.com/pclick.php?p=49338340&amp;a=111402&amp;m=80&amp;platform=cs</sawdeeplink>
        <sawthumburl>http://images.productserve.com/thumb/80/49338340.jpg</sawthumburl>
        <fprice>54.99</fprice>
      </oproduct>
      <itotalcount>6350</itotalcount>
    </ns1:getproductlistresponse>
  </soap-env:body>
</soap-env:envelope>

推荐答案

您能否说明您使用的是哪个版本的 PHP(4 或 5)?还有一个特殊的原因为什么您不想/不能使用 PHP 5 的 SOAP 扩展?了解这些信息应该有助于我们为您提供更好的答案.

Could you clarify which version of PHP you're using (4 or 5)? Also is there a particular reason why you don't want to/can't use PHP 5's SOAP extension? Knowing this information should help us to give you a better answer.

上面的代码示例不起作用的原因是您正在查找 oproduct 节点的错误命名空间.虽然根节点包含在 SOAP 命名空间中,但 oproduct 位于http://v3.core.com.productserve.com/"命名空间下.您还需要在 XPath 查询中使用名称空间别名.试试这个,虽然我还没有测试过:

The reason the code sample above isn't working is that you're looking in the wrong namespace for the oproduct nodes. While the root node is contained in the SOAP namespace the oproduct ones are under the "http://v3.core.com.productserve.com/" namespace. You also need to use the namespace alias in the XPath query. Try this, although I haven't tested it:

$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('ns', 'http://v3.core.com.productserve.com/');
foreach ($xml->xpath('//ns:oproduct') as $item)
{
  // do something
}

希望这能解决您眼前的问题.

Hopefully that will solve your immediate problem.

编辑感谢您的澄清.同样,未经测试,但也许这会奏效:

EDIT Thanks for the clarification. Again, untested but maybe this would work:

$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('ns', 'http://v3.core.com.productserve.com/');
foreach ($xml->xpath('/soapenv:envelope/soapenv:body/ns:getproductlistresponse/ns:oproduct') as $item)
    {
      // do something
    }

也许您需要在查询中从根节点到soap-Env:body 再到oproduct 节点.希望这会奏效.

Perhaps you need to go from the root node to the soap-Env:body to the oproduct nodes in the query. Hopefully that will work.

进一步我想我刚刚破解了这个.试试下面的代码:

Further edit: I think I've just cracked this. Try the following code:

$xml = simplexml_load_string($response);
$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 />';
}

不是最漂亮的代码,但它有效.我希望让它与 XPath 查询一起工作,但这超出了我对 XPath 的基本了解.也许其他人可以使用 XPath 发布答案?

Not the prettiest code but it works. I was hoping to get this to work with an XPath query but it was beyond my rudimentary knowledge of XPath. Perhaps someone else can post an answer using XPath?

这篇关于如何在没有 SoapClient 的情况下解析 SOAP 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在没有 SoapClient 的情况下解析 SOAP 响应

基础教程推荐