Error regarding CADSUS webservice in PHP

Asked

Viewed 1,855 times

2

Hello, everybody.

I’m trying to consume function "pesquisar" of the CADSUS bus, but I receive as a response the error

"Forced circuit exception";

The code used follows below, using Laravel 5.6 and PHP 7.1:

Route::get('/ws/teste', function () {
    try {
        $opts = array(
            'http' => array(
                'user_agent' => 'PHPSoapClient'
            )
        );
        $context = stream_context_create($opts);
        $wsdlUrl = 'https://servicoshm.saude.gov.br/cadsus/CadsusService/v5r0?wsdl';
        $soapClientOptions = array(
            'trace' => 1,
            'stream_context' => $context,
            'cache_wsdl' => WSDL_CACHE_NONE,
            'Username' => 'CADSUS.CNS.PDQ.PUBLICO',
            'Password' => 'kUXNmiiii#RDdlOELdoe00966'
        );
        $client = new SoapClient($wsdlUrl, $soapClientOptions);
        $parameters = array(
            'CNESUsuario' => array(
                'CNES' => '6963447',
                'Usuario' => 'LEONARDO',
                'Senha' => ''
            ),
            'FiltroPesquisa' => array(
                'nomeCompleto' => 'SERGIO ARAUJO CORREIA LIMA',
                'tipoPesquisa' => 'IDENTICA'
            ),
            'higienizar' => 0
        );
        $result = $client->pesquisar($parameters);
        print_r($result);
    }
    catch(\Exception $e) {
        echo $e->getMessage();
    } });

If anyone knows what might be going on, please help me!

2 answers

2


Good night, I had a similar problem, and I had to pretty much alter your code, - the only way I was able to authenticate was by sending in the header with the namespaces defined in the example

  • passes the data with array of the very problem 'Encoding: Object has no ', better go by class, follows below code that worked 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>';
}
?>

Search By CNS (Unica that returns complete data) XML example

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:cad="http://servicos.saude.gov.br/cadsus/v5r0/cadsusservice" xmlns:cnes="http://servicos.saude.gov.br/wsdl/mensageria/v5r0/cnesusuario" xmlns:cns="http://servicos.saude.gov.br/schema/cadsus/v5r0/cns">
   <soap:Header>
	  <wsse:Security soap: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>CADSUS.CNS.PDQ.PUBLICO</wsse:Username>
	  <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">kUXNmiiii#RDdlOELdoe00966</wsse:Password>
	  </wsse:UsernameToken>
	  </wsse:Security>
   </soap:Header>
   <soap:Body>
      <cad:requestConsultar>
         <cnes:CNESUsuario>
            <cnes:CNES>?</cnes:CNES>
            <cnes:Usuario>?</cnes:Usuario>
            <!--Optional:-->
            <cnes:Senha>?</cnes:Senha>
         </cnes:CNESUsuario>
         <cns:CNS>
            <cns:numeroCNS>AQUI_NUMERO_CNS</cns:numeroCNS>
         </cns:CNS>
      </cad:requestConsultar>
   </soap:Body>
</soap:Envelope>

  • could put the code within the answer, as we cannot rely on links, since they may be canceled in the future

  • @A programmer has already entered the code in the answer.

  • It worked perfectly! Thank you very much! Only one question came up: sending user data and password via xml header do not know a security flaw? If someone was sniffing the net, they couldn’t capture the packet and visualize?

  • Lucas Maia, I can’t tell if there’s a security problem, but I don’t think so. and in the technical specification you request to send, in the example of page 13 http://datasus.saude.gov.br/images/teroperability/Especificao%20Tecnica%20para%20Integration%20CADSUS%20V5.pdf

  • Opa, @Andrericardoalvesdossantos, thank you again!

  • complementing: I found that to return the complete data use the operation consult of the web service informing only the letter sus. but the structure has to be exact I will put in the initial answer.

Show 1 more comment

0

Good evening, everyone! Do all your requests come back? since at first it returns error php soapclient ssl Connection reset by peer , but sending again and going.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.