转换SOAP返回数组

时间:2018-08-01 15:57:35

标签: php arrays xml curl soap

我从SOAP Web服务获得了此回报:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:historyResponse xmlns:ns2="XXXXXXXXXX">
         <return>
            <userFullname>AGENT</userFullname>
            <date>2018-08-01T17:24:20.948+02:00</date>
            <stateName>Préparé</stateName>
         </return>
         <return>
            <userFullname>PARAPHEUR</userFullname>
            <date>2018-08-01T17:24:21.039+02:00</date>
            <stateName>Envoyé pour signature</stateName>
         </return>
         <return>
            <userFullname>Administrator</userFullname>
            <date>2018-08-01T17:24:21.228+02:00</date>
            <stateName>Signé</stateName>
         </return>
         <return>
            <userFullname>PARAPHEUR</userFullname>
            <date>2018-08-01T17:24:21.324+02:00</date>
            <stateName>Classé</stateName>
         </return>
      </ns2:historyResponse>
   </soap:Body>
</soap:Envelope>

当我通过PHP和CURL使用此Web服务时,我只会得到一个像这样的字符串(var dump):

string(728) "<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:historyResponse xmlns:ns2="http://sei.ws.fast.cdc.com/"><return><userFullname>AGENT EDISSYUM</userFullname><date>2018-08-01T17:24:20.948+02:00</date><stateName>Préparé</stateName></return><return><userFullname>PARAPHEUR </userFullname><date>2018-08-01T17:24:21.039+02:00</date><stateName>Envoyé pour signature</stateName></return><return><userFullname>Administrator </userFullname><date>2018-08-01T17:24:21.228+02:00</date><stateName>Signé</stateName></return><return><userFullname>PARAPHEUR </userFullname><date>2018-08-01T17:24:21.324+02:00</date><stateName>Classé</stateName></return></ns2:historyResponse></soap:Body></soap:Envelope>"

我怎么能从这个字符串中得到一个“返回”数组?

1 个答案:

答案 0 :(得分:1)

您可以使用simplexml_load_string()和json函数将其转换为数组。 (请注意,由于某些原因该功能无法识别名称空间的xml,因此您需要替换冒号)

$response = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:historyResponse xmlns:ns2="http://sei.ws.fast.cdc.com/"><return><userFullname>AGENT EDISSYUM</userFullname><date>2018-08-01T17:24:20.948+02:00</date><stateName>Préparé</stateName></return><return><userFullname>PARAPHEUR </userFullname><date>2018-08-01T17:24:21.039+02:00</date><stateName>Envoyé pour signature</stateName></return><return><userFullname>Administrator </userFullname><date>2018-08-01T17:24:21.228+02:00</date><stateName>Signé</stateName></return><return><userFullname>PARAPHEUR </userFullname><date>2018-08-01T17:24:21.324+02:00</date><stateName>Classé</stateName></return></ns2:historyResponse></soap:Body></soap:Envelope>';

//I'm not the author of this regex ;)
$xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);

$xml = simplexml_load_string($xml);
$json = json_encode($xml);
$responseArray = json_decode($json,true);

echo '<pre>';
print_r($responseArray);

希望有帮助!!随时问您是否有任何疑问:)