0
I am trying to consume a SOAP webservice using PHP but the access returns me message stating that the user validation was not done correctly. The XML I need to send is the following:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:aut="http://servicos.saude.gov.br/wsdl/cmd/mensageria/v1r0/autenticacaocmd" xmlns:con="http://servicos.saude.gov.br/cmd/v1/contatoassistencialservice">
<soap:Header xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:aut="http://servicos.saude.gov.br/wsdl/cmd/mensageria/v1r0/autenticacaocmd">
<wsse:Security soap:mustUnderstand="true" 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-91A6C9ADF539A7475514438193926252">
<wsse:Username>XXXXXXXXXXX</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXXXXXXXXX</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
<aut:AutenticacaoCMD>
<aut:cpf>XXXXXXXXX</aut:cpf>
<aut:senha>XXXXXXXX</aut:senha>
</aut:AutenticacaoCMD>
</soap:Header>
<soap:Body>
<con:RequestCancelarContatoAssistencial>
<con:codigoContatoAssistencial>XXXXXXXXXXXX</con:codigoContatoAssistencial>
</con:RequestCancelarContatoAssistencial>
</soap:Body>
</soap:Envelope>
If I take this XML and test using Soapui the service is consumed correctly, but direct in PHP gives error.
The last attempt was to send the integer XML as parameter:
$soapServer = 'https://servicoshm.saude.gov.br/cmd/ContatoAssistencialService/v1r0?wsdl';
$ns = 'http://servicos.saude.gov.br/wsdl/cmd/mensageria/v1r0/autenticacaocmd';
$arrContextOptions=array("ssl"=>array( "verify_peer"=>false, "verify_peer_name"=>false,'crypto_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT));
$options = array(
'soap_version'=>SOAP_1_2,
'exceptions'=>true,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE,
'stream_context' => stream_context_create($arrContextOptions)
);
$function = 'cancelarContatoAssistencial';
try {
$soapClient = new SoapClient($soapServer,$options);
$xml = '
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:aut="http://servicos.saude.gov.br/wsdl/cmd/mensageria/v1r0/autenticacaocmd" xmlns:con="http://servicos.saude.gov.br/cmd/v1/contatoassistencialservice">
<soap:Header xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:aut="http://servicos.saude.gov.br/wsdl/cmd/mensageria/v1r0/autenticacaocmd">
<wsse:Security soap:mustUnderstand="true" 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-91A6C9ADF539A7475514438193926252">
<wsse:Username>XXXXXXXX</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXXXXX</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
<aut:AutenticacaoCMD>
<aut:cpf>XXXXXXXXXX</aut:cpf>
<aut:senha>XXXXXXXXX</aut:senha>
</aut:AutenticacaoCMD>
</soap:Header>
<soap:Body>
<con:RequestCancelarContatoAssistencial>
<con:codigoContatoAssistencial>XXXXXXXXXX</con:codigoContatoAssistencial>
</con:RequestCancelarContatoAssistencial>
</soap:Body>
</soap:Envelope>
';
$retorno = $soapClient->cancelarContatoAssistencial(new SoapVar($xml, XSD_ANYXML));
} catch ( SoapFault $fault ) {
echo '<pre>' ;
print_r($soapClient->__getLastRequest());
print_r($fault);
echo '</pre>' ;
}
I also tried using the setSoapHeaders function to mount the header:
$soapServer = 'https://servicoshm.saude.gov.br/cmd/ContatoAssistencialService/v1r0?wsdl';
$ns = 'http://servicos.saude.gov.br/wsdl/cmd/mensageria/v1r0/autenticacaocmd';
$arrContextOptions=array("ssl"=>array( "verify_peer"=>false, "verify_peer_name"=>false,'crypto_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT));
$options = array(
'soap_version'=>SOAP_1_2,
'exceptions'=>true,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE,
'stream_context' => stream_context_create($arrContextOptions)
);
$location = array('location' => 'https://servicoshm.saude.gov.br/cmd/ContatoAssistencialService/v1r0' );
$function = 'cancelarContatoAssistencial';
$arguments['RequestCancelarContatoAssistencial'] = array( 'codigoContatoAssistencial' => 'XXXXXXXXXXXXXXXXXXXXX' );
try {
$soapClient = new SoapClient($soapServer,$options);
$security = '
<wsse:Security soap:mustUnderstand="true" 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-91A6C9ADF539A7475514438193926252">
<wsse:Username>XXXXXXXXX</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXXXXXX</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
';
$auth = '
<aut:AutenticacaoCMD>
<aut:cpf>XXXXXXXX</aut:cpf>
<aut:senha>XXXXXXXXXX</aut:senha>
</aut:AutenticacaoCMD>
';
$header = array();
$header[] = new SoapHeader($ns, 'Security', new SoapVar($security, XSD_ANYXML), true);
$header[] = new SoapHeader($ns, 'AutenticacaoCMD', new SoapVar($auth, XSD_ANYXML), false);
$soapClient->__setSoapHeaders($header);
$retorno = $soapClient->__soapCall($function, $arguments, $location);
} catch ( SoapFault $fault ) {
echo '<pre>' ;
print_r($soapClient->__getLastRequest());
print_r($fault);
echo '</pre>' ;
}
Following the guidelines of POST I consumed the service using the suggested code (using the real data) and the same returned me validation error. When debugging XML I realized that the tag Usernametoken was missing.
I used the code below and the service was successfully consumed. Thank you very much for your help.
$soapServer = 'https://servicoshm.saude.gov.br/cmd/ContatoAssistencialService/v1r0?wsdl';
$arrContextOptions = array("ssl" => array("verify_peer" => false , "verify_peer_name" => false , 'crypto_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT));
$options = array(
'soap_version'=>SOAP_1_2,
'exceptions'=>true,
'trace'=>1,
'cache_wsdl' => WSDL_CACHE_NONE,
'stream_context' => stream_context_create($arrContextOptions)
);
try {
$securityNS = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
$auth = new stdClass();
$auth->Username = new SoapVar('XXXXXXXX', XSD_STRING, null, null, 'Username', $securityNS);
$auth->Password = new SoapVar('XXXXXXXX', XSD_STRING, null, null, 'Password', $securityNS);
$token = new stdClass();
$token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, null, null, 'UsernameToken', $securityNS);
$security = new SoapVar($token, SOAP_ENC_OBJECT, null, null, 'Security', $securityNS);
$headers[] = new SoapHeader($securityNS , 'Security' , $security , true);
$soapClient = new SoapClient($soapServer , $options);
$ns = 'http://servicos.saude.gov.br/wsdl/cmd/mensageria/v1r0/autenticacaocmd';
$auth = new stdClass();
$auth->cpf = new SoapVar('XXXXXXXX', XSD_STRING, null, null, 'cpf', $ns);
$auth->senha = new SoapVar('XXXXXXXX', XSD_STRING, null, null, 'senha', $ns);
$autenticacaoCMD = new SoapVar($auth, SOAP_ENC_OBJECT, null, null, 'AutenticacaoCMD', $ns);
$headers[] = new SoapHeader($ns , 'AutenticacaoCMD' , $autenticacaoCMD , false);
$soapClient->__setSoapHeaders($headers);
$arguments = new stdClass();
$arguments->codigoContatoAssistencial = 'XXXXXXXXXXXXXXXXXXXX';
$retorno = $soapClient->cancelarContatoAssistencial($arguments);
} catch ( SoapFault $fault ) {
echo '<pre>' ;
var_dump($fault);
echo '</pre>' ;
}
Post the shapes you have tried to consume so far.
– Gabriel Heming
@Gabrielheming follows reply below
– Bruno Augusto
Do not create answers to insert new information, always edit the question. The first question you should understand is that Soapclient abstracts almost all of the work (not all of it) of manipulating XML, so you don’t send XML to consume WS. The only point that should be treated differently are the headers. Once I have time, or another user, can write a response.
– Gabriel Heming