Problem consuming webservice from DataSUS in PHP

1

Follow the code:

            <?php   
            $client = new SoapClient('https://servicoshm.saude.gov.br/cadsus/CadsusService/v5r0?wsdl');
                                $function = "Pesquisar";
                                $arguments = array("Pesquisar" => 
                                                array(
                                                    "CNESUsuario" => array("CNES" =>"6963447", "Usuario"=>"LEONARDO", "Senha"=>"?"),
                                                    "FiltroPesquisa" => array("CPF"=> array("numeroCPF"=>"66105234368"), "tipoPesquisa"=>"IDENTICA"),
                                                    "higienizar" => "0"
                                                )
                                            );
                                $result = $client->__soapCall($function,$arguments);
                                print($result); 

            ?>

I'm getting the following error:

                Fatal error: Uncaught SoapFault exception: [env:Receiver ] nested fault: XML parse failed: libxml error: : level: 3, code: 5, file: none, line: 1, str1: "", str2: "", str3: "", int1: 0, int2: 37, message: Extra content at the end of the document in *...\cadsus.php:12 Stack trace: #0 *...\cadsus.php(12): SoapClient->__soapCall('Pesquisar', Array) #1 {main} thrown in *...\cadsus.php on line 12

Any solution?

    
asked by anonymous 30.07.2018 / 14:58

2 answers

1

I had this error because it lacked the header with the authentication, to solve manually includes with soapVar. follows example code that works for me.

<?php
try {
    $wsdlUrl = 'https://servicoshm.saude.gov.br/cadsus/CadsusService/v5r0?wsdl';
    $wsUser = 'CADSUS.CNS.PDQ.PUBLICO';
    $passWs = 'kUXNmiiii#RDdlOELdoe00966';
    $soapClientOptions = array(
	'trace' => 1,
	'cache_wsdl' => WSDL_CACHE_NONE
    );
    $client = new SoapClient($wsdlUrl, $soapClientOptions);
    $xmlheader = '
<wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <wsse:UsernameToken wsu:Id="UsernameToken-F6C95C679D248B6E3F143032021465917">
        <wsse:Username>' . $wsUser . '</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' . $passWs . '</wsse:Password>
    </wsse:UsernameToken>
</wsse:Security>
';
    $header = new SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', new \SoapVar($xmlheader, XSD_ANYXML), false);
    $client->__setSoapHeaders($header);

    $requestPesquisar = new stdClass();
    $requestPesquisar->CNESUsuario = new stdClass();
    $requestPesquisar->CNESUsuario->CNES = '6963447';
    $requestPesquisar->CNESUsuario->Usuario = 'LEONARDO';
    $requestPesquisar->CNESUsuario->Senha = '?';
    $requestPesquisar->FiltroPesquisa = new stdClass();
    $requestPesquisar->FiltroPesquisa->nomeCompleto = new stdClass();
    $requestPesquisar->FiltroPesquisa->nomeCompleto->Nome = 'SERGIO ARAUJO CORREIA LIMA';
    $requestPesquisar->FiltroPesquisa->tipoPesquisa = 'IDENTICA';
    $requestPesquisar->higienizar = '0';
    $result = $client->pesquisar($requestPesquisar);
    if ($result) {
	echo '<pre>', print_r($result), '</pre>';
    } else {
	echo '<h2>Request:</h2>';
	echo '<pre>', print_r($client->__getLastRequest()), '</pre>';
	echo '<h2>Header:</h2>';
	echo '<pre>', print_r($client->__getLastRequestHeaders()), '</pre>';
	echo '<h2>Response:</h2>';
	echo '<pre>', print_r($client->__getLastResponse()), '</pre>';
    }
} catch (Exception $e) {
    echo '<pre>', print_r($e), '<pre>';
}
?>

Now I'm with another problem, this webservice does not return complete patient data, eg does not return address, municipality of residence etc ...

In the PDQ / PIX version it is more complete but I could not execute with PHP.

I hope it helps.

Hugs!

    
02.08.2018 / 06:17
0

Unfortunately I do not have privileges to comment, I tried some things here to try to help you, but it seems to me that the problem is in the respssa xml of webservice. I rewrote the code a bit differently from yours, and I always had the same result.

The Xml library is complaining that the xml document of the responder has more than one root and XMl semantically speaking can only have one root.

Bad

<?xml version="1.0"?>
<status>success</status>
<format>xml</format>

Good

<?xml version="1.0"?>
<response>
  <status>success</status>
  <format>xml</format>
</response>

The response xml should be the following in PHP

<soap:Fault>
         <soap:Code>
            <env:Value xmlns:env="http://www.w3.org/2003/05/soap-envelope">env:Sender</env:Value>
         </soap:Code>
         <soap:Reason>
            <soap:Text xml:lang="pt-BR">Uma ou mais regras negociais foram violadas, verifique a lista de erros.</soap:Text>
         </soap:Reason>
         <soap:Detail>
            <msf:MSFalha xmlns:msf="http://servicos.saude.gov.br/wsdl/mensageria/falha/v5r0/msfalha">
               <msf:Mensagem xmlns:men="http://servicos.saude.gov.br/wsdl/mensageria/falha/v5r0/mensagem">
                  <men:codigo>OSB_SEM_AUTENTICACAO</men:codigo>
                  <men:descricao>As credenciais informadas não são válidas</men:descricao>
               </msf:Mensagem>
            </msf:MSFalha>
         </soap:Detail>
      </soap:Fault>

This xml has code, reason, Detail that everything indicates as root

If you get this xml, which SOAPUI displays as a response and put in an XMl validator, you will notice that it is wrong, you can use this w3school for this, I'm not an XML expert, this is just the result of a search and debug

Below the code I wrote if you're curious

<?php

//318707

$pesquisar = new stdClass();
$pesquisar->CNESUsuario = new stdClass();
$pesquisar->CNESUsuario->CNES = "6963447";
$pesquisar->CNESUsuario->Usuario = "LEONARDO";
$pesquisar->CNESUsuario->Senha = "";
$pesquisar->FiltroPesquisa = new stdClass();
$pesquisar->FiltroPesquisa->CPF = new stdClass();
$pesquisar->FiltroPesquisa->CPF->numeroCPF = "66105234368";
$pesquisar->FiltroPesquisa->tipoPesquisa = "IDENTICA";
$pesquisar->higienizar = false;


$ver = "v5r0";

$url = "https://servicoshm.saude.gov.br/cadsus/CadsusService/$ver?wsdl";

// Opções de configurações do cliente WSDL
$options = [
    'soap_version' => SOAP_1_1, // SOAP_1_2
    'encoding' => 'UTF-8',
];

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

$result  = $soap->Pesquisar($pesquisar);
print $result;

I took a look at the documentation of this webservice and noticed that there is a video about how to implement this solution in Java and Soapui is done in java, I do not know what magic java does to understand this answer.     

31.07.2018 / 04:43