soapheader authenticate using php [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP SoapClient and a complex header
I have this header structure:
<soapenv:Header>
<wsse:Security xmlns:wsse=”http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd” soapenv:mustUnderstand=”0”>
<wsse:UsernameToken>
<wsse:Username Type=”http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken”>votre_login</wsse:Username>
<wsse:Password Type=”http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText”>votre_password</wsse:Password>
</wsse:UsernameToken>
</wsse :Security>
</soapenv :Header>
How I put this in php using __setSoapHeaders() method?
I tried:
$auth = array(
'UsernameToken' => array(
'Username' => '*****',
'Password' => '*****'
)
);
$header = new SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','Security',$auth, 0);
$client->__setSoapHeaders($header);
and I have this error:
com.ibm.wsspi.wssecurity.SoapSecurityException: WSEC5509E: Un jeton de
sécurité dont le type est
[http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken]
est requis.

From: http://php.net/manual/en/soapclient.setsoapheaders.php#93460
To create complex SOAP Headers, you can do something like this:
Required SOAP Header:
<soap:Header>
<RequestorCredentials xmlns="http://namespace.example.com/">
<Token>string</Token>
<Version>string</Version>
<MerchantID>string</MerchantID>
<UserCredentials>
<UserID>string</UserID>
<Password>string</Password>
</UserCredentials>
</RequestorCredentials>
</soap:Header>
Corresponding PHP code:
$ns = 'http://namespace.example.com/'; //Namespace of the WS.
//Body of the Soap Header.
$headerbody = array('Token' => $someToken,
'Version' => $someVersion,
'MerchantID'=>$someMerchantId,
'UserCredentials'=>array('UserID'=>$UserID,
'Password'=>$Pwd));
//Create Soap Header.
$header = new SOAPHeader($ns, 'RequestorCredentials', $headerbody);
//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);

Related

How to create a SOAP request using PHP with a XML example?

I have the XML below:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:proc="http://servicos.saude.gov.br/sigtap/v1/procedimentoservice" xmlns:grup="http://servicos.saude.gov.br/schema/sigtap/procedimento/nivelagregacao/v1/grupo" xmlns:sub="http://servicos.saude.gov.br/schema/sigtap/procedimento/nivelagregacao/v1/subgrupo" xmlns:com="http://servicos.saude.gov.br/schema/corporativo/v1/competencia" xmlns:pag="http://servicos.saude.gov.br/wsdl/mensageria/v1/paginacao">
<soap:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="Id-0001334008436683-000000002c4a1908-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>SIGTAP.PUBLICO</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">sigtap#2015public</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soap:Body>
<proc:requestPesquisarProcedimentos>
<grup:codigoGrupo>05</grup:codigoGrupo>
<sub:codigoSubgrupo>04</sub:codigoSubgrupo>
<com:competencia>201501</com:competencia>
<pag:Paginacao>
<pag:registroInicial>01</pag:registroInicial>
<pag:quantidadeRegistros>20</pag:quantidadeRegistros>
<pag:totalRegistros>100</pag:totalRegistros>
</pag:Paginacao>
</proc:requestPesquisarProcedimentos>
</soap:Body>
</soap:Envelope>
How to create a PHP request?
I am trying:
$soapClientOptions = array(
'Username' => 'SIGTAP.PUBLICO',
'Password' => 'sigtap#2015public'
);
$client = new SoapClient("https://servicoshm.saude.gov.br/sigtap/ProcedimentoService/v1?wsdl", $soapClientOptions);
$params = array(
'codigoGrupo' => '05',
'competencia' => '201901',
'Paginacao' => array(
'registroInicial' => '01',
'quantidadeRegistros' => '20',
'totalRegistros' => '100'
)
);
$response = $client->__soapCall("pesquisarProcedimentos", array($params));
var_dump($response);
I am noob on SOAP and I have no idea to how create a correct code. The code that I use is not working. I have a Forced circuit exception.
Any help?
If you don’t want to wonder how to construct the PHP request using an OOP approach + the native PHP SoapClient class with autocompletion in PhpStorm or any decent IDE, I strongly advise you to use a WSDL to PHP generator.
Try the https://github.com/WsdlToPhp/PackageGenerator.
For the WsSecurity header, you can also use the https://github.com/WsdlToPhp/WsSecurity.

Set xml rquest header in non-wsdl soap client using php

I am trying to make a non-wsdl SOAP client call using php. My code is something like this:
try {
$URL = 'http://example.com/webservices/security/accesscontrol.asmx';
$sc = new SoapClient(null, array(
'location' => $URL,
'uri' => 'http://example.com/webservices/security/',
'trace' => 1
));
$usertoken = array('UserNameToken' =>
array(
'UserName' => 'test',
'Password' => 'test123'
));
$header = new SoapHeader('http://www.docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', $usertoken);
$sc->__setSoapHeaders($header);
$test = $sc->__soapCall("AuthenticateClient",
array(),
array('soapaction' => 'http://example.com/webservices/security/AuthenticateClient')
);
If I debug and see the Last request header part of xml it looks like this:
<SOAP-ENV:Header>
<ns2:Security>
<item><key>UserNameToken</key><value><item><key>UserName</key><value>test</value></item><item><key>Password</key><value>test123</value></item></value></item>
</ns2:Security>
</SOAP-ENV:Header>
But if I use wsdl file, the xml header looks like this:
<SOAP-ENV:Header>
<ns2:Security>
<ns2:UserNameToken>
<ns2:UserName>test</ns2:UserName>
<ns2:Password>test123</ns2:Password>
</ns2:UserNameToken>
</ns2:Security>
</SOAP-ENV:Header>
How can I make the header part like above using non-wsdl SOAP client call? Becasue its not working and giving an error that is caused by "if either the UserName Token or the UserName was not provided in the AuthenticateClient Soap Header Request"
Thanks in advance for your help.
Please note that I have changed the URL and password intentionally as I can not disclose them.
You can create the part of the header manually and insert it into the SoapHeader, try to do something like this:
$URL = 'http://example.com/webservices/security/accesscontrol.asmx';
$soapClient = new SoapClient(null, array(
'location' => $URL,
'uri' => 'http://example.com/webservices/security/',
'trace' => 1
));
$headerPart = '
<SOAP-ENV:Header>
<ns2:Security>
<ns2:UserNameToken>
<ns2:UserName>DASKO</ns2:UserName>
<ns2:Password>welcome1</ns2:Password>
</ns2:UserNameToken>
</ns2:Security>
</SOAP-ENV:Header>
';
$soapVarHeader = new SoapVar($headerPart, XSD_ANYXML, null, null, null);
$header = new SoapHeader(
'http://www.docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', // Namespace - namespace of the WebService
'Security',
$soapVarHeader,
false // mustunderstand
);
$soapClient->__setSoapHeaders($header);

PHP call to WCF - soap header issue

I am making some PHP application and I want to use my WCF service, which is working good. I need to make soap call, and set header to something like this:
<s:Header>
<a:Action s:mustUnderstand="1">GetPage</a:Action>
<a:MessageID>1</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">http://someurl.com</a:To>
In my php file where I want to make the call I have:
$ns = 'http://www.w3.org/2005/08/addressing'; //Namespace of the WS.
//Body of the Soap Header.
$headerbody = array('MessageID' => '1',
'Action' => 'GetPage',
'To' => 'http://someurl.com');
//Create Soap Header.
$header = new SOAPHeader($ns, $headerbody);
//set the Headers of Soap Client.
$soapClient->__setSoapHeaders($header);
$GetPageResponse = $soapClient->GetPage($GetPageRequest);
When I go to the logs of my service, I see that the message that came in looks like:
<SOAP-ENV:Envelope xmlns:SOAP- ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Flow/2012/10">
<SOAP-ENV:Header>
<To SOAP-ENV:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://someurl.com</To>
<Action SOAP-ENV:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">urn:Flow/2012/10/GetPage</Action>
</SOAP-ENV:Header>
</SOAP-ENV:Envelope>
$GetPageRequest is for now irrelevant. So, in my header there is no MessageID. What am I missing here? Did I set something wrong? Also, how to correctly set part 'ReplyTo'?
Not sure why MessageID is not getting through in your output yet, but adding ReplyTo->Address is as simple as adding an sub-array to your header array.
$headerbody = array('MessageID' => '1',
'Action' => 'GetPage',
'To' => 'http://someurl.com',
'ReplyTo' => array (
'Address' => 'http://www.w3.org/2005/08/addressing/anonymous'
)
);

How to set SOAP header on PHP to authenticate a web service?

I need to set the SOAP header into this format:
<soapenv:Header>
<wsse:Security 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-45">
<wsse:Username>XXXXX</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXX</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">OxWtCYYj1cX7HiZeMEqorw==</wsse:Nonce>
<wsu:Created>2013-09-18T07:25:50.227Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
I've tried authenticating the webservice using the code below but it didn't work.
$momurl = "https://integrationdev.momentum.co.za/sales/CRMService/CRMLeadService_v1_0/";
$client = new SoapClient(
"$momurl",
array(
'trace' => 1,
'login' => $username,
'password' => $password
)
);
Any help would be appreciated. Thanks in advance.
You need to use SoapClient::__setSoapHeader(). Like this:
$security = array(
'Username'=>'XXXXX',
'Password'=>'XXXXX',
'Nonc'=> 'OxWtCYYj1cX7HiZeMEqorw==',
'Created' => '2013-09-18T07:25:50.227Z',
'UsernameToken' => NULL
);
$header = new SoapHeader('wsse','Security',$security, false);
$client->__setSoapHeaders($header);

PHP : Subnode of SOAP Header element is not found or recognized

I'm trying to call a SOAP Adonix X3 web service by using a php client.
For testing, I used SOAP UI and it worked well ; this is the xml request :
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:a="http://www.adonix.com/WSS"
xmlns:XS="http://www.w3.org/2001/XMLSchema"
xmlns:XI="http://www.w3.org/2001/XMLSchema-instance">
<S:Header>
<a:CAdxCallingContext>
<a:codeLang XI:type="XS:string">FRA</a:codeLang>
<a:codeUser XI:type="XS:string">ADM</a:codeUser>
<a:password XI:type="XS:string">XXX</a:password>
<a:poolAlias XI:type="XS:string">TEST</a:poolAlias>
<a:requestConfig XI:type="XS:string">trace</a:requestConfig>
</a:CAdxCallingContext>
</S:Header>
<S:Body>
<a:runXml S:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<publicName XI:type="XS:string">RECH_OF</publicName>
<inputXml XI:type="XS:string">
<![CDATA[
<PARAM>
<GRP ID="GRP1">
<FLD NAME="XITMREF">PSFIN00153</FLD>
<FLD NAME="XFLUX">recycle</FLD>
<FLD NAME="XOPENUM">15</FLD>
</GRP>
</PARAM>
]]>
</inputXml>
</a:runXml>
</S:Body>
</S:Envelope>
but trying to do the same call in php :
$sh_param = array(
'codeLang' => 'FRA',
'codeUser' => 'ADM',
'password' => 'XXX',
'poolAlias' => 'TEST',
'requestConfig ' => 'trace'
);
$ns = 'http://www.adonix.com/WSS';
$headers = new SoapHeader($ns, 'CAdxCallingContext', $sh_param, false);
// Prepare Soap Client
$soapClient->__setSoapHeaders(array($headers));
$at_param2 = array(
'XITMREF' => 'PSFIN00153',
'XFLUX' => 'recycle',
'XOPENUM' => '15');
// Setup the RemoteFunction parameters
$ap_param = array(
'publicName' => 'RECH_OF',
'inputXml' => array($at_param2));
$info = $soapClient->__call("runXml", array($ap_param));
I get the following error :
3 - Le Header element [http://www.adonix.com/WSS][CAdxCallingContext] du message Soap n'a pas de fils [codeLang].
this means
The Header element [http://www.adonix.com/WSS][CAdxCallingContext] of the Soap message has no son [codeLang]
It seems the server doesn't find the subnode of the header...
any idea ?
Thanks
The problem you're having is because the X3 Web Service is unable to identify header parameters without the namespace reference
Moreover, you should use a SoapVar instead of the basic array to build a correct header
So you should try something like this
$ns = 'http://www.adonix.com/WSS';
$headerParams = array('ns1:codeLang' => 'FRA',
'ns1:codeUser' => 'ADM',
'ns1:password' => 'XXX',
'ns1:poolAlias' => 'TEST',
'ns1:requestConfig' => 'trace');
$soapStruct = new SoapVar($headerParams, SOAP_ENC_OBJECT);
$header = new SoapHeader($ns, 'CAdxCallingContext', $soapStruct, false);
Good Luck
Al.

Categories