J
Evaluating what is described via WSDL and interpreted via SoapUI, the code below meets the need:<?php
$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 {
$soapClient = new SoapClient($soapServer , $options);
$ns = 'http://servicos.saude.gov.br/wsdl/cmd/mensageria/v1r0/autenticacaocmd';
$auth = new stdClass();
$auth->cpf = new SoapVar('111.111.111-11', XSD_STRING, null, null, 'cpf', $ns);
$auth->senha = new SoapVar('pass', XSD_STRING, null, null, 'senha', $ns);
$autenticacaoCMD = new SoapVar($auth, SOAP_ENC_OBJECT, null, null, 'AutenticacaoCMD', $ns);
$header = new SoapHeader($ns , 'AutenticacaoCMD' , $autenticacaoCMD , false);
$soapClient->__setSoapHeaders($header);
$arguments = new stdClass();
$arguments->codigoContatoAssistencial = 'código';
$retorno = $soapClient->cancelarContatoAssistencial($arguments);
} catch ( SoapFault $fault ) {
echo '<pre>' ;
var_dump($fault);
echo '</pre>' ;
}
The XML generated for WS consumption is as follows:<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://servicos.saude.gov.br/cmd/v1/contatoassistencialservice" xmlns:ns2="http://servicos.saude.gov.br/wsdl/cmd/mensageria/v1r0/autenticacaocmd">
<SOAP-ENV:Header>
<ns2:AutenticacaoCMD>
<ns2:cpf>111.111.111-11</ns2:cpf>
<ns2:senha>pass</ns2:senha>
</ns2:AutenticacaoCMD>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:RequestCancelarContatoAssistencial>
<ns1:codigoContatoAssistencial>código</ns1:codigoContatoAssistencial>
</ns1:RequestCancelarContatoAssistencial>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
If you really need to add the tag Security, just transform the variable $header in an array (now it will be called $headers) and add how many headers are needed to add a new header:<?php
$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('user', XSD_STRING, null, null, 'Username', $securityNS);
$auth->Password = new SoapVar('pass', XSD_STRING, null, null, 'Password', $securityNS);
$usernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, null, null, 'UsernameToken', $securityNS);
$security = new SoapVar($usernameToken, 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('111.111.111-11', XSD_STRING, null, null, 'cpf', $ns);
$auth->senha = new SoapVar('pass', 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 = 'código';
$retorno = $soapClient->cancelarContatoAssistencial($arguments);
} catch ( SoapFault $fault ) {
echo '<pre>' ;
var_dump($fault);
echo '</pre>' ;
}
The XML generated for WS consumption is as follows:<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://servicos.saude.gov.br/cmd/v1/contatoassistencialservice" xmlns:ns2='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' xmlns:ns3="http://servicos.saude.gov.br/wsdl/cmd/mensageria/v1r0/autenticacaocmd">
<SOAP-ENV:Header>
<ns2:Security SOAP-ENV:mustUnderstand="1">
<ns2:Username>user</ns2:Username>
<ns2:Password>pass</ns2:Password>
</ns2:Security>
<ns3:AutenticacaoCMD>
<ns3:cpf>111.111.111-11</ns3:cpf>
<ns3:senha>pass</ns3:senha>
</ns3:AutenticacaoCMD>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:RequestCancelarContatoAssistencial>
<ns1:codigoContatoAssistencial>código</ns1:codigoContatoAssistencial>
</ns1:RequestCancelarContatoAssistencial>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>