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

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);

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.

PHP Request for SOAP XML

i've got this XML and it works in soapUi
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="xxx" xmlns:data="xxx">
<soapenv:Header/>
<soapenv:Body>
<web:FindArticles>
<web:request>
<data:culture_id>de-DE</data:culture_id>
<data:vehicle_search_parameters>
<data:address>
<!--Optional:-->
<data:countries>
<!--Zero or more repetitions:-->
<data:country_id>D</data:country_id>
</data:countries>
<!--Optional:-->
</data:address>
<data:dealer_id>XXX</data:dealer_id>
<data:show_dealer_vehicles>true</data:show_dealer_vehicles>
</data:vehicle_search_parameters>
</web:request>
</web:FindArticles>
</soapenv:Body>
</soapenv:Envelope>
But I can not fugure out how to call it via PHP. I'm trying something like this, but get errors...
<?php
$wsdl = 'XXX';
$options = array(
'login' => 'XXX',
'password' => 'XXX',
'trace'=>true
);
$params = array(
'culture_id' => 'de-DE',
'country_id' => 'D',
'dealer_id' => 'XXX',
'show_dealer_vehicles' => true
);
try {
$soap = new SoapClient($wsdl, $options);
$data = $soap->FindArticles($params);
}
catch(Exception $e) {
die($e->getMessage());
}
var_dump($data);
die;
It looks, that there is some kind of nested functions - how have I call it via PHP? I've tried smth like $soap->FindArticles('request', $params), but it still does not work

PHP Soap authentication header

Sample Request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="https://ws.intermedia.net/Account/Management">
<soapenv:Header>
<AuthentificationInfo>
<login>[PLRAdminUserName]</login>
<password>[PLRAdminPassword]</password>
<accountID>[accountID]</accountID>
</AuthentificationInfo>
</soapenv:Header>
<soapenv:Body>
<GetAccount>
<accountID>[accountID]</accountID>
</GetAccount>
</soapenv:Body>
</soapenv:Envelope>
WSDL: https://controlpanel.msoutlookonline.net/WebServices/Account/AccountService.asmx?WSDL
PHP:
ini_set("soap.wsdl_cache_enabled", "0");
$wsdl = "https://controlpanel.msoutlookonline.net/WebServices/Account/AccountService.asmx?WSDL";
$ns = 'https://ws.intermedia.net/Account/Management';
$client = new SoapClient($wsdl, array(
"trace" => 1,
"exceptions" => 0
));
$login = 'xxxx';
$password = 'xxxx';
$partnerID = 1234;
$accountID = 12345678;
$headerBody = array('AuthentificationInfo'=>array(
'login' => $login,
'password' => $password,
'accountID' => $partnerID
));
$header = new SoapHeader($ns, 'AuthentificationInfo', $headerBody);
$client->__setSoapHeaders($header);
$client->__soapCall("echoVoid", array(null));
$value = $client->GetAccount($accountID);
I'm getting the following error message:
soap:ServerServer was unable to process request. ---> Access denied; Code: 0x0008
Can anyone see anything wrong with the code?
Try with
$headerBody = array(
'login' => $login,
'password' => $password,
'accountID' => $partnerID);
just for anyone else that may ever run across this:
I changed
$ns = 'https://ws.intermedia.net/Account/Management';
to:
$ns = 'http://schemas.msoutlookonline.net';
I had an incorrect namespace.
Also Mikaël DELSOL's answer helped as I didn't need the array('AuthentificationInfo'=> part.
Also didn't need: $client->__soapCall("echoVoid", array(null));
Thank you!

soapheader authenticate using php [duplicate]

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);

Missing UsernameToken element

I am getting a request in the following method but the soap message does not appear to contain the UsernameToken Element:
$policy = new WSPolicy(
array(
'useUsernameToken'=>true
)
);
$security = new WSSecurityToken(
array(
'user'=>$username,
'passwordType'=>'PlainText',
'password'=>$password
)
);
// create client in WSDL mode
$client = new WSClient(
array (
'wsdl'=>$service_wsdl,
'to'=>$service_url,
'policy'=>$policy,
'securityToken'=>$security,
'trace'=>1
)
);
$proxy = $client->getProxy();
$proxy->Ping();
The request that is produced looks like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soapenv:mustUnderstand="1">
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<ns1:Ping xmlns:ns1="http://streamlinedsalestax.org/efile"/>
</soapenv:Body>
</soapenv:Envelope>
You'll notice that the UsernameToken element is completely missing.
how about this.. added 'security' to WSPolicy
<?php
$policy = new WSPolicy(
array('security' => array(
'useUsernameToken'=>true
))
);
$security = new WSSecurityToken(
array(
'user'=>$username,
'passwordType'=>'PlainText',
'password'=>$password
)
);
// create client in WSDL mode
$client = new WSClient(
array (
'wsdl'=>$service_wsdl,
'to'=>$service_url,
'policy'=>$policy,
'securityToken'=>$security,
'trace'=>1
)
);
$proxy = $client->getProxy();
$proxy->Ping();

Categories