【问题标题】:Parse XML SOAP to Object JSON with PHP 7使用 PHP 7 将 XML SOAP 解析为对象 JSON
【发布时间】:2017-05-28 10:45:56
【问题描述】:

我有如下问题,需要将SOAP响应从$response = $client -> __ getLastResponse();并转换为 JSON 对象。这是我使用上述方法在 PHP 中得到的响应图像。

stdClass 返回 $result:

stdClass Object ( [RealizarConsultaSQLResult] => AEC4 - AEC7 - )

响应 SOAP:

<NewDataSet><Resultado> <CODTURMA>AEC4</CODTURMA><NOME>-</NOME></Resultado><Resultado><CODTURMA>AEC7</CODTURMA><NOME>-</NOME> 
</Resultado></NewDataSet>

观察:我的代码工作正常,只需要收到以下格式的答案:(预期输出):

{
  "NewDataSet": {
    "Resultado": [
      {
        "CODTURMA": "AEC4",
        "NOME": "-"
      },
      {
        "CODTURMA": "AEC7",
        "NOME": "-"
      }
    ]
  }
}

我的代码

   <?php
    require("wsdls.php");
    //Cabeçalho de autenticação básica no SOAP
    $soapParams = array('login' => $usuario,
                     'password' => base64_decode($pass),
               'authentication' => SOAP_AUTHENTICATION_BASIC,
                        'trace' => 1,
                   'exceptions' => 0
    );

    $client = new SoapClient($WsdlRMSQL, $soapParams); 
    $params = array('codSentenca' => 'TOTVS.MDA.402', 'codColigada'=>'1', 'codSistema'=>'S','parameters'=>'CODCOLIGADA=1;CODFILIAL=1;IDPERLET=2;IDHABILITACAOFILIAL=10');

    //Resultado do Webservice
    $result = $client->realizarConsultaSQL($params);

    //Resposta do SOAP
    $response = $client->__getLastResponse();
    //$xmlString = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
    //var_dump($response);
    print_r($response);


    ?>

Image form SOAP response

【问题讨论】:

  • 共享响应字符串而不是图像以及您想要提取的预期输出。
  • 不要发布字符串的图片,把字符串写在你的问题中。
  • 响应:AEC4-AEC7 -
  • 你能分享你想从这个回复中提取哪些信息吗?
  • Sahil, { "NewDataSet": { "Resultado": [ { "CODTURMA": "AEC4", "NOME": "-" }, { "CODTURMA": "AEC7", "NOME ": "-" } ] } }

标签: php json xml soap


【解决方案1】:
try{    
    $xml = $client->__getLastResponse();
    // SimpleXML seems to have problems with the colon ":" in the <xxx:yyy> response tags, so take them out

    $xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xml);
    $xml = simplexml_load_string($xml);
    $json = json_encode($xml);
    $responseArray = json_decode($json,true);

    // dd($responseArray['soapenvHeader']['headResponseHeader']['headStatusMessages']['headMessageDescription']);

    Log::info("bill payment response".json_encode($result));
    Log::info("bill payment response xml".$client->__getLastResponse());

    if(!empty($responseArray['soapenvBody'])):
        return redirect()->back()->with('status', $responseArray['soapenvHeader']['headResponseHeader']['headStatusMessages']['headMessageDescription']);
    else:
        return redirect()->back()->with('err', "We could not complete your request, please try again");
    endif;

} catch (\SoapFault $fault) {
    Log::info("bill payment error".json_encode($fault));
    return redirect()->back()->with('err', $fault->getMessage());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    相关资源
    最近更新 更多