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'
)
);
Related
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.
After over a half a day of trying and reading tutorials on creating a simple SOAP client, I am no closer to retrieving a request from API I attempting to work with.
WSDL: http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL
I can make the request from SOAP UI with the following simple SOAP request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pub="http://publicapi.ekmpowershop.com/">
<soapenv:Header/>
<soapenv:Body>
<pub:GetOrders>
<!--Optional:-->
<pub:GetOrdersRequest>
<!--Optional:-->
<pub:APIKey>myApiKey</pub:APIKey>
</pub:GetOrdersRequest>
</pub:GetOrders>
</soapenv:Body>
</soapenv:Envelope>
This above returns the expected data.
When it comes to translating the request into a PHP I have the following:
$wsdl = 'http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$param = array('GetOrdersRequest' => array(
'APIKey' => 'myApiKey'
)
);
$resp = $client->GetOrders();
print_r($param);
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());
If put the $param into the GetOrders function then, it breaks and nothing happens.
Even if I use an array in the $client->GetOrders(array('someArry' => $param)) then response and request still always looks the same and looks like the body of the SOAP request is never created:
Request:
?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://publicapi.ekmpowershop.com/"><SOAP-ENV:Body><ns1:GetOrders/></SOAP-ENV:Body></SOAP-ENV:Envelope>
Response:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetOrdersResponse xmlns="http://publicapi.ekmpowershop.com/"><GetOrdersResult><Status>Failure</Status><Errors><string>Object reference not set to an instance of an object.</string></Errors><Date>2017-04-03T16:00:42.9457446+01:00</Date><TotalOrders>0</TotalOrders><TotalCost xsi:nil="true" /></GetOrdersResult></GetOrdersResponse></soap:Body></soap:Envelope>
If anyone can shed some light on what I am doing wrong here that would be real big help?
P.S My experience of SOAP in PHP is limited as I am used to SOAP in a java env. Thanks
You need to pass the parameters into the $client->GetOrders() call. Also the WSDL defines some required parameters, so a minimal example is something like:
$wsdl = 'http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$param = array(
'GetOrdersRequest' => array(
'APIKey' => 'dummy-key',
'CustomerID' => 1,
'ItemsPerPage' => 1,
'PageNumber' => 1,
)
);
$resp = $client->GetOrders($param);
print_r($param);
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());
This gives the error response:
<Errors><string>Invalid character in a Base-64 string.</string></Errors>
which presumably is because my API key is invalid.
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);
From debugging another application, I found that it sends the following xml to a soap server (code parts in this example are minimized, the xml is about 200 lines long:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:m0="http://schemas.ccs.nl/datacatalogus/modellen/modelrelatie">
<SOAP-ENV:Header>
<m:header xmlns:m="http://schemas.ccs.nl/soap">
<m:account>account</m:account>
<m:naam>naam_header</m:naam>
<m:wachtwoord>wachtwoord</m:wachtwoord>
<m:bedrijfsnummer>bedrijfsnummer</m:bedrijfsnummer>
<m:tussenpersoonnummer>tussenpersoonnummer</m:tussenpersoonnummer>
</m:header>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<m:RelatieMuteren xmlns:m="http://schemas.ccs.nl/services/relatieservice">
<m:relatie pc="W">
<m0:adres>adres</m0:adres>
</m:relatie>
</m:RelatieMuteren>
</SOAP-ENV:Body>
Obviously, the part between <m0:adres></m0:adres> is a lot larger and corresponds to my data that is stored in an array. However, if I try to send the request using __soapCall, PHP builds the following request:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://schemas.ccs.nl/datacatalogus/modellen/modelrelatie"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns2="http://schemas.ccs.nl/services/relatieservice" xmlns:ns3="http://schemas.ccs.nl/soap">
<SOAP-ENV:Header>
<ns3:header>
<ns3:account>account</ns3:account>
<ns3:naam>naam</ns3:naam>
<ns3:wachtwoord>wachtwoord</ns3:wachtwoord>
<ns3:bedrijfsnummer>bedrijfsnummer</ns3:bedrijfsnummer>
<ns3:tussenpersoonnummer>tussenpersoonnummer</ns3:tussenpersoonnummer>
</ns3:header>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns2:RelatieMuteren>
<ns2:relatie pc="I">
<ns1:adres>Postbus 53</ns1:adres>
</ns2:relatie>
</ns2:RelatieMuteren>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
As you can see, the second xml is quite different from the first. Can anyone explain me why it is different and how I can create the first type of xml?
I use the following code to do the request:
$client = new SoapClient( "http://www.cdsverzekeringen.nl/ws_prod/services/RelatieService.asmx?WSDL", array( 'trace' => 1 ) );
$aHeader = array(
'account' => "PRIVATE",
'naam' => "PRIVATE",
'wachtwoord' => "PRIVATE",
'bedrijfsnummer' => "PRIVATE",
'tussenpersoonnummer' => "PRIVATE",
);
$client->__setSoapHeaders( new SoapHeader( "http://schemas.ccs.nl/soap", 'header', $aHeader ) );
$vtResult = $client->__soapCall( "RelatieMuteren", array( $aRelatieInfo ) );
The $aRelatieInfo array is formatted like this:
array
'relatie' =>
array
'adres' => string 'Postbus 53' (length=10)
I hope someone can help me out. Thanks in advance!
How about using SoabVar API like this,
$aHeader = '
<m:header xmlns:m="http://schemas.ccs.nl/soap">
<m:account>account</m:account>
<m:naam>naam_header</m:naam>
<m:wachtwoord>wachtwoord</m:wachtwoord>
<m:bedrijfsnummer>bedrijfsnummer</m:bedrijfsnummer>
<m:tussenpersoonnummer>tussenpersoonnummer</m:tussenpersoonnummer>
</m:header>';
$soap_var_header = new SoapVar( $aHeader , XSD_ANYXML, null, null, null);
$client->__setSoapHeaders( new SoapHeader( "http://schemas.ccs.nl/soap", 'm', $soap_var_header ) );
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.