PHP SoapClient 没有获得子结构

我正在尝试获取 SOAP 服务的响应,但无法获取子集合数据。当我使用肥皂客户端软件调用 ws 方法时,我得到下一个响应:


<WSGLMSuit.METHODNAME xmlns="http://tempuri.org/">

         <Sdtpolizadetalle>

            <Empresa>1</Empresa>

            <DscEmpresa>TEST</DscEmpresa>

            <Rama>22</Rama>

            <DscRama>COMBINADO FAMILIAR</DscRama>

            <Poliza>000000</Poliza>

            <DscRiesgo/>

            <InicioVigencia>2019-03-18</InicioVigencia>

            <FinVigencia>2019-09-18</FinVigencia>

            <Productor>3311</Productor>

            <NombreProductor>TEST</NombreProductor>

            <Tomador>

               <CodTomador>336028</CodTomador>

               <NombreTomador>TEST</NombreTomador>

               <Domicilio>SAAVEDRA 1174 Dpto. 0</Domicilio>

               <Localidad>TRES ARROYOS</Localidad>

               <CodigoPostal>7500</CodigoPostal>

            </Tomador>

            <DscMoneda>PESOS</DscMoneda>

            <CantidadCuotas>3</CantidadCuotas>

            <Suplementos>

               <Suplemento>

                  <Suplemento>0</Suplemento>

                  <TipoOperacion>02</TipoOperacion>

                  <SubTipoOperacion>000</SubTipoOperacion>

                  <DscOperacion>GENERAL</DscOperacion>

                  <InicioVigencia>2019-03-18</InicioVigencia>

                  <FinVigencia>2019-09-18</FinVigencia>

                  <Prima>2515.95</Prima>

                  <Premio>3104.68</Premio>

                  <Cuotas>

                     <Cuota>

                        <NroCuota>1</NroCuota>

                        <Vencimiento>2019-03-18</Vencimiento>

                        <Estado>Pagada</Estado>

                        <Importe>519.68</Importe>

                        <NroCupon>1</NroCupon>

                     </Cuota>

                     <Cuota>

                        <NroCuota>2</NroCuota>

                        <Vencimiento>2019-04-18</Vencimiento>

                        <Estado>Vencida</Estado>


LEATH
浏览 131回答 1
1回答

UYOU

最后我得到了一个解决方案。我找到了一个将 XML 字符串解析为数组的函数。所以我所做的是将响应作为 XML 获取,将其保存到一个文件中并使用该函数对其进行解析。代码胜于文字:function xmlToArray($xml, $options = array()){&nbsp; $defaults&nbsp; &nbsp; &nbsp; &nbsp;= array(&nbsp; &nbsp; 'namespaceSeparator' => ':',&nbsp; &nbsp; 'attributePrefix'&nbsp; &nbsp; => '@',&nbsp; &nbsp; 'alwaysArray'&nbsp; &nbsp; &nbsp; &nbsp; => array(),&nbsp; &nbsp; 'autoArray'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => true,&nbsp; &nbsp; 'textContent'&nbsp; &nbsp; &nbsp; &nbsp; => '$',&nbsp; &nbsp; 'autoText'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> true,&nbsp; &nbsp; 'keySearch'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => false,&nbsp; &nbsp; 'keyReplace'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> false&nbsp; );&nbsp; $options&nbsp; &nbsp; &nbsp; &nbsp; = array_merge($defaults, $options);&nbsp; $namespaces&nbsp; &nbsp; &nbsp;= $xml->getDocNamespaces();&nbsp; $namespaces[''] = null;&nbsp; $attributesArray = array();&nbsp; foreach ($namespaces as $prefix => $namespace) {&nbsp; &nbsp; foreach ($xml->attributes($namespace) as $attributeName => $attribute) {&nbsp; &nbsp; &nbsp; if ($options['keySearch']) $attributeName =&nbsp; &nbsp; &nbsp; &nbsp; str_replace($options['keySearch'], $options['keyReplace'], $attributeName);&nbsp; &nbsp; &nbsp; $attributeKey&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $options['attributePrefix']&nbsp; &nbsp; &nbsp; &nbsp; . ($prefix ? $prefix . $options['namespaceSeparator'] : '')&nbsp; &nbsp; &nbsp; &nbsp; . $attributeName;&nbsp; &nbsp; &nbsp; $attributesArray[$attributeKey] = (string)$attribute;&nbsp; &nbsp; }&nbsp; }&nbsp; $tagsArray = array();&nbsp; foreach ($namespaces as $prefix => $namespace) {&nbsp; &nbsp; foreach ($xml->children($namespace) as $childXml) {&nbsp; &nbsp; &nbsp; $childArray = xmlToArray($childXml, $options);&nbsp; &nbsp; &nbsp; list($childTagName, $childProperties) = each($childArray);&nbsp; &nbsp; &nbsp; if ($options['keySearch'])&nbsp; &nbsp; &nbsp; &nbsp; $childTagName = str_replace($options['keySearch'], $options['keyReplace'], $childTagName);&nbsp; &nbsp; &nbsp; if ($prefix)&nbsp; &nbsp; &nbsp; &nbsp; $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName;&nbsp; &nbsp; &nbsp; if (!isset($tagsArray[$childTagName])) {&nbsp; &nbsp; &nbsp; &nbsp; $tagsArray[$childTagName] =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in_array($childTagName, $options['alwaysArray']) || !$options['autoArray']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? array($childProperties)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : $childProperties;&nbsp; &nbsp; &nbsp; } elseif (&nbsp; &nbsp; &nbsp; &nbsp; is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName])&nbsp; &nbsp; &nbsp; &nbsp; === range(0, count($tagsArray[$childTagName]) - 1)&nbsp; &nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; $tagsArray[$childTagName][] = $childProperties;&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp; $textContentArray = array();&nbsp; $plainText&nbsp; &nbsp; &nbsp; &nbsp; = trim((string)$xml);&nbsp; if ($plainText !== '') $textContentArray[$options['textContent']] = $plainText;&nbsp; $propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || ($plainText === '')&nbsp; &nbsp; ? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText;&nbsp; return array(&nbsp; &nbsp; $xml->getName() => $propertiesArray&nbsp; );}$params&nbsp; = array('key' => 'value') // params needed to make the request$options = array(&nbsp; 'trace'&nbsp; &nbsp; &nbsp; => 1,&nbsp; 'exceptions' => true);try {&nbsp; $soap = new SoapClient($url, $options);&nbsp; $soap->method($params); // replace method name&nbsp; $xmlResponse = $soap->__getLastResponse();} catch (Exception $e) {&nbsp; die(&nbsp; json_encode(&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; 'error' => 'Cannot request to WS. ' . $e->getMessage()&nbsp; &nbsp; ]&nbsp; ));// save the response into an xml file for parse&nbsp;// it and return its content as jsonif (file_put_contents('response.xml', $xmlResponse)) {&nbsp; $xmlNode&nbsp; &nbsp;= simplexml_load_file($this->tempPath);&nbsp; $arrayData = xmlToArray($xmlNode);&nbsp; unlink('response.xml');&nbsp; return json_encode($arrayData, JSON_PRETTY_PRINT);} else {&nbsp; return json_encode(['error' => 'Error saving the response into file.']);}
打开App,查看更多内容
随时随地看视频慕课网APP