亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP SoapClient 沒有獲得子結構

PHP SoapClient 沒有獲得子結構

PHP
LEATH 2021-06-21 16:23:51
我正在嘗試獲取 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>
查看完整描述

1 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

最后我得到了一個解決方案。我找到了一個將 XML 字符串解析為數組的函數。所以我所做的是將響應作為 XML 獲取,將其保存到一個文件中并使用該函數對其進行解析。代碼勝于文字:


function xmlToArray($xml, $options = array())

{

  $defaults       = array(

    'namespaceSeparator' => ':',

    'attributePrefix'    => '@',

    'alwaysArray'        => array(),

    'autoArray'          => true,

    'textContent'        => '$',

    'autoText'           => true,

    'keySearch'          => false,

    'keyReplace'         => false

  );

  $options        = array_merge($defaults, $options);

  $namespaces     = $xml->getDocNamespaces();

  $namespaces[''] = null;


  $attributesArray = array();

  foreach ($namespaces as $prefix => $namespace) {

    foreach ($xml->attributes($namespace) as $attributeName => $attribute) {

      if ($options['keySearch']) $attributeName =

        str_replace($options['keySearch'], $options['keyReplace'], $attributeName);

      $attributeKey                   = $options['attributePrefix']

        . ($prefix ? $prefix . $options['namespaceSeparator'] : '')

        . $attributeName;

      $attributesArray[$attributeKey] = (string)$attribute;

    }

  }


  $tagsArray = array();

  foreach ($namespaces as $prefix => $namespace) {

    foreach ($xml->children($namespace) as $childXml) {

      $childArray = xmlToArray($childXml, $options);

      list($childTagName, $childProperties) = each($childArray);


      if ($options['keySearch'])

        $childTagName = str_replace($options['keySearch'], $options['keyReplace'], $childTagName);


      if ($prefix)

        $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName;


      if (!isset($tagsArray[$childTagName])) {

        $tagsArray[$childTagName] =

          in_array($childTagName, $options['alwaysArray']) || !$options['autoArray']

            ? array($childProperties)

            : $childProperties;

      } elseif (

        is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName])

        === range(0, count($tagsArray[$childTagName]) - 1)

      ) {

        $tagsArray[$childTagName][] = $childProperties;

      } else {

        $tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties);

      }

    }

  }


  $textContentArray = array();

  $plainText        = trim((string)$xml);

  if ($plainText !== '') $textContentArray[$options['textContent']] = $plainText;


  $propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || ($plainText === '')

    ? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText;


  return array(

    $xml->getName() => $propertiesArray

  );

}


$params  = array('key' => 'value') // params needed to make the request

$options = array(

  'trace'      => 1,

  'exceptions' => true

);


try {

  $soap = new SoapClient($url, $options);

  $soap->method($params); // replace method name

  $xmlResponse = $soap->__getLastResponse();

} catch (Exception $e) {

  die(

  json_encode(

    [

      'error' => 'Cannot request to WS. ' . $e->getMessage()

    ]

  )

);


// save the response into an xml file for parse 

// it and return its content as json

if (file_put_contents('response.xml', $xmlResponse)) {

  $xmlNode   = simplexml_load_file($this->tempPath);

  $arrayData = xmlToArray($xmlNode);

  unlink('response.xml');

  return json_encode($arrayData, JSON_PRETTY_PRINT);

} else {

  return json_encode(['error' => 'Error saving the response into file.']);

}



查看完整回答
反對 回復 2021-06-25
  • 1 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號