How do i create this specific xml output by SOAP using SoapHeader and __setSoapHeaders? I can't do the same XML like i want. i don't want this ns1 and ns2 in envelope tag, and in header i need this Action SOAP-ENV:mustUnderstand="1" ...
This is my code:
try{
$client = new SoapClient('https://thesite.com.br/wcf/SvcContratos.svc?wsdl', array('trace' => 1,'use' => SOAP_LITERAL));
$usuario='user_1';
$senha='1234';
$tipo='1';
$header = new SoapHeader("http://schemas.microsoft.com/ws/2005/05/addressing/none","Action", "http://tempuri.org/ISvcContratos/GerarToken");
$client->__setSoapHeaders($header);
$params = new SoapVar("<objLogin xmlns:d4p1='http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><d4p1:DsSenha>".$senha."</d4p1:DsSenha><d4p1:DsUsuario>".$usuario."</d4p1:DsUsuario><d4p1:IdTipoConsulta>".$tipo."</d4p1:IdTipoConsulta></objLogin>", XSD_ANYXML);
$data = $client->GerarToken($params);
echo $client->__getLastRequest();
}catch(SoapFault $fault){
echo $client->__getLastRequest()."<br>".$fault->getMessage();
}
With this php code i had this wrong XML:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" xmlns:ns2="http://schemas.microsoft.com/ws/2005/05/addressing/none">
<SOAP-ENV:Header>
<ns2:Action>http://tempuri.org/ISvcContratos/GerarToken</ns2:Action>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<GerarToken>
<objLogin xmlns:d4p1='http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
<d4p1:DsSenha>1234</d4p1:DsSenha>
<d4p1:DsUsuario>user_1</d4p1:DsUsuario>
<d4p1:IdTipoConsulta>1</d4p1:IdTipoConsulta>
</objLogin>
</GerarToken>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I need to send this XML by soap:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/ISvcContratos/GerarToken</Action>
</s:Header>
<s:Body>
<GerarToken xmlns="http://tempuri.org/">
<objLogin xmlns:d4p1="http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:DsSenha>1234</d4p1:DsSenha>
<d4p1:DsUsuario>USER_1</d4p1:DsUsuario>
<d4p1:IdTipoConsulta>1</d4p1:IdTipoConsulta>
</objLogin>
</GerarToken>
</s:Body>
</s:Envelope>
well, finally I got an affirmative answer from the server, which opens the doors for me now to try to consume the wsdl follows below the code that I used to solve the problem:
try{
$client = new SoapClient('https://thesite.com.br/wcf/SvcContratos.svc?wsdl', array('trace' => 1,'use' => SOAP_LITERAL, 'style' => SOAP_DOCUMENT,));
$usuario='user_1';
$senha='1234';
$params = new SoapVar("<GerarToken xmlns='http://tempuri.org/'><objLogin xmlns:d4p1='http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><d4p1:DsUsuario>".$usuario."</d4p1:DsUsuario><d4p1:DsSenha>".$senha."</d4p1:DsSenha><d4p1:IdTipoConsulta>Data</d4p1:IdTipoConsulta></objLogin></GerarToken>", XSD_ANYXML);
$data = $client->GerarToken($params);
$xml = json_decode(json_encode($data),true);
print_r($xml);
echo $client->__getLastRequest();
}catch(SoapFault $fault){
echo $client->__getLastRequest()."<br>".$fault->getMessage();
}
Related
I have to make a SOAP request but I'm facing some problems because using SoapClient I can't get the same XML that is supposed.
This is the correct xml request
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header>
<wsse:Security
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:Username>10983</wsse:Username>
<wsse:Password>test2019</wsse:Password>
<Context>10000Hoteis</Context>
</wsse:Security>
</soap-env:Header>
<soap-env:Body>
<OTA_ReadRQ xmlns:ns="http://www.opentravel.org/OTA/2003/05/common"
xmlns="http://www.opentravel.org/OTA/2003/05" TimeStamp="2015-07-16T06:38:10.60">
<ReadRequests>
<HotelReadRequest>
<TPA_Extensions>
<RequestType>GetCities</RequestType>
<CountryCode>PT</CountryCode>
</TPA_Extensions>
</HotelReadRequest>
</ReadRequests>
</OTA_ReadRQ>
</soap-env:Body>
</soap-env:Envelope>
And this is my current php code
$xmlheader = <<<XML
<wsse:Security
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:Username>myusername</wsse:Username>
<wsse:Password>mypassword</wsse:Password>
<Context>10000Hoteis</Context>
</wsse:Security>
XML;
$xmlbody = <<<XML
<OTA_ReadRQ xmlns:ns="http://www.opentravel.org/OTA/2003/05/common"
xmlns="http://www.opentravel.org/OTA/2003/05" TimeStamp="2015-07-16T06:38:10.60">
<ReadRequests>
<HotelReadRequest>
<TPA_Extensions>
<RequestType>GetCities</RequestType>
<CountryCode>PT</CountryCode>
</TPA_Extensions>
</HotelReadRequest>
</ReadRequests>
</OTA_ReadRQ>
XML;
$headerXML = new \SoapVar($xmlheader,XSD_ANYXML);
$body = new \SoapVar($xmlbody,XSD_ANYXML);
$client = new \SoapClient(null, ['location' => 'http://10000hoteis.com.pt/NewAvailabilityServlet/staticdata/OTA2014A', 'uri' =>'http://schemas.xmlsoap.org/soap/envelope/' ,'trace' => true]);
$header = new \SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', $headerXML);
$client->__setSoapHeaders($header);
$result = $client->OTA_ReadRQ($body);
But this don't work because put an extra
<SOAP-ENV:OTA_ReadRQ>
between the body tag and
<OTA_ReadRQ xmlns:ns="http://www.opentravel.org/OTA/2003/05/common"
xmlns="http://www.opentravel.org/OTA/2003/05" TimeStamp="2015-07-16T06:38:10.60">
How can I do this request correctly?
In PHP I need to create a soap-xml request like
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header xmlns:NS1="urn:UCoSoapDispatcherBase" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<NS1:TAuthenticationHeader xsi:type="NS1:TAuthenticationHeader">
<UserName xsi:type="xsd:string">user</UserName>
<Password xsi:type="xsd:string">pass</Password>
</NS1:TAuthenticationHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<NS2:ExecuteRequest xmlns:NS2="urn:UCoSoapDispatcherCustomLink-ICustomLinkSoap">
<ARequest xsi:type="xsd:string">
<?xml version="1.0" encoding="windows-1252"?> <EoCustomLinkRequestDateTime Type="TEoCustomLinkRequestDateTime"></EoCustomLinkRequestDateTime>
</ARequest>
</NS2:ExecuteRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
But the problem is to get param in the header (like xmlns:NS1...) and body (like xmlns:NS2...) tags
With my script I get NS1 and NS2 swapped in the first tag after header and body. results in <NS1:TAuthenticationHeader> .... and . </NS2:ExecuteRequest>
My php script:
<?php
$wsdl_url = 'http://xxx.xxx.xxx.xxx:yyyy/wsdl/ICustomLinkSoap';
$location = 'http://xxx.xxx.xxx.xxx:yyyy/soap/ICustomLinkSoap';
$action = 'ExecuteRequest';
$version = SOAP_1_1;
$one_way = 0;
$soapClient = new SoapClient($wsdl_url, array(
'cache_wsdl' => WSDL_CACHE_NONE,
'trace' => true,
'encoding' => 'ISO-8859-1',
'exceptions' => true,
));
$auth = (object)array(
'UserName'=>'xxxx',
'Password'=>'yyyy'
);
$header = new SoapHeader($wsdl_url,'TAuthenticationHeader',$auth,false);
$soapClient->__setSoapHeaders($header);
$request1 = '
<ARequest>
<?xml version="1.0" encoding="windows-1252"?>
<EoCustomLinkRequestDateTime Type="TEoCustomLinkRequestDateTime" xsi:noNamespaceSchemaLocation="GdxEoStructures.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</EoCustomLinkRequestDateTime></ARequest>
';
$xmlVar = new SoapVar($request1, XSD_ANYXML);
$result = $soapClient->__SoapCall(
'ExecuteRequest',
array($xmlVar)
);
// show result in xml-file
$f = fopen("./soap-request2.xml", "w");
xx = serialize($soapClient->__getLastRequest());
fwrite($f, $xx);
?>
Result:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="urn:UCoSoapDispatcherCustomLink-ICustomLinkSoap"
xmlns:ns2="http://xxx.xxx.xxx.xxx:yyy/wsdl/ICustomLinkSoap"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<ns2:TAuthenticationHeader>
<UserName>xxxx</UserName>
<Password>yyyy</Password>
</ns2:TAuthenticationHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:ExecuteRequest>
<ARequest>
<?xml version="1.0" encoding="windows-1252"?>
<EoCustomLinkRequestDateTime Type="TEoCustomLinkRequestDateTime" xsi:noNamespaceSchemaLocation="GdxEoStructures.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</EoCustomLinkRequestDateTime>
</ARequest>
</ns1:ExecuteRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Please help to how to do so?
Eric
But the problem is to get param in the header (like xmlns:NS1...) and
body (like xmlns:NS2...) tags With my script I get NS1 and NS2 swapped
in the first tag after header and body. results in
.... and .
It's not a problem, because swapped not only namespaces of elements but also its definitions at the head of SOAP-ENV:Envelope
Your first xml
xmlns:NS1="urn:UCoSoapDispatcherBase"
xmlns:NS2="urn:UCoSoapDispatcherCustomLink-ICustomLinkSoap"
Compare with generated by php
xmlns:ns1="urn:UCoSoapDispatcherCustomLink-ICustomLinkSoap"
xmlns:ns2="http://xxx.xxx.xxx.xxx:yyy/wsdl/ICustomLinkSoap"
My SOAP Request
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://ws.dgpys.deloitte.com" xmlns:ns2="ws.apache.org/namespaces/axis2">
<env:Header>
<ns2:ServiceGroupId>
<BOGUS>urn:uuid:7C2F61BDE7CB9D9C6D1424938568724</BOGUS>
</ns2:ServiceGroupId>
</env:Header>
<env:Body>
<ns1:getGunlukParametreRapor>
<date>2015-02-22T00:00Z</date>
</ns1:getGunlukParametreRapor>
</env:Body>
</env:Envelope>
Expected SOAP Request
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ws="http://ws.dgpys.deloitte.com">
<soap:Header>
<axis2:ServiceGroupId xmlns:axis2="http://ws.apache.org/namespaces/axis2">urn:uuid:479731898147E116AD1424691518968</axis2:ServiceGroupId>
</soap:Header>
<soap:Body>
<ws:getGunlukParametreRapor>
<date>2015-02-22T00:00Z</date>
</ws:getGunlukParametreRapor>
</soap:Body>
</soap:Envelope>
Tried with following codes:
$options = array(
'trace' => 1,
'exceptions' => 1,
'soap_version' => SOAP_1_2
);
$client = new SoapClient("http://dgpysws.pmum.gov.tr/dgpys/services/EVDServis.wsdl", $options);
$p1 = new stdCLass();
$p1->loginMessage = new stdCLass();
$p1->loginMessage->UserName = new stdCLass();
$p1->loginMessage->UserName->v = "Username";
$p1->loginMessage->Password = new stdCLass();
$p1->loginMessage->Password->v = "Passwor";
$client->login($p1);
$headers[] = new SoapHeader('http//ws.apache.org/namespaces/axis2', 'ServiceGroupId', "UNIQUEID", false);
$client->__setSoapHeaders($headers);
$result = $client->getGunlukParametreRapor(array('date' => '2015-02-22T00:00Z'));
Question is:
These SOAP requests are same?
I'm using SOAP_1_2 and it should be like Expected SOAP Request but my request doesnt looks like to expected format. Missing where?
How can i get the output like as expected?
Note: dgpysws.pmum.gov.tr wsdl address is private area.
They are not the same. To get rid of the BOGUS node you need to use this:
$strHeaderComponent_Session = "<SessionHeader><ServiceGroupId>$theVarWithTheIDGoesHere</ServiceGroupId></SessionHeader>";
$objVar_Session_Inside = new SoapVar($strHeaderComponent_Session, XSD_ANYXML,
null, null, null);
$objHeader_Session_Outside = new SoapHeader('http//ws.apache.org/namespaces/axis2',
'SessionHeader', $objVar_Session_Inside);
// More than one header can be provided in this array.
$client->__setSoapHeaders(array($objHeader_Session_Outside));
try the following
$ns = 'http//ws.apache.org/namespaces/axis2'; //Namespace of the WS.
//Body of the Soap Header.
$headerbody = array('ServiceGroupId' => $UNIQUEID_Token);
//Create Soap Header.
$header = new SOAPHeader($ns, 'axis2', $headerbody);
//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://ws.dgpys.deloitte.com" xmlns:ns2="ws.apache.org/namespaces/axis2">
<env:Header>
<ns2:ServiceGroupId>
urn:uuid:7C2F61BDE7CB9D9C6D1424938568724
</ns2:ServiceGroupId>
</env:Header>
<env:Body>
<ns1:getGunlukParametreRapor>
<date>2015-02-22T00:00Z</date>
</ns1:getGunlukParametreRapor>
</env:Body>
</env:Envelope>
And
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ws="http://ws.dgpys.deloitte.com">
<soap:Header>
<axis2:ServiceGroupId xmlns:axis2="http://ws.apache.org/namespaces/axis2">urn:uuid:479731898147E116AD1424691518968</axis2:ServiceGroupId>
</soap:Header>
<soap:Body>
<ws:getGunlukParametreRapor>
<date>2015-02-22T00:00Z</date>
</ws:getGunlukParametreRapor>
</soap:Body>
</soap:Envelope>
Are the same.
env=soap, ns2=ws and ns2=axis2. You can have any prefix to refer to these namespaces as you like. Once you assign the prefix you just refer to it using that in the other places. Only diff was the bogus tag tin first request. Just remove that.
I am trying to consume a web service with no success. It looks like the xml is no properly set.
This is my PHP code:
<?php
$wsdlUrl = "http://api.clickatell.com/soap/webservice.php?WSDL";
$serviceUrl = "http://api.clickatell.com/soap/webservice.php";
$request = array("user"=>"anyuser",
"password"=>"asdsda",
"api_id"=>"1234"
);
var_dump($request);
try {
$client = new SoapClient($wsdlUrl, array("trace" => 1, "location" => $serviceUrl));
var_dump($client);
echo "\n";
$response = $client->auth($request);
var_dump($response);
var_dump($client->__getLastRequest());
echo "\n";
}
catch(Exception $exp) {
echo "EXCEPTION";
}
?>
And the SOAP packet is:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.clickatell.com/soap/webservice" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:auth>
<api_id xsi:type="xsd:int">1</api_id>
<user xsi:nil="true"/>
<password xsi:nil="true"/>
</ns1:auth>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
What is wrong with the code that causes that api_id, user and password are not being sent?
If you check out var_dump($client->__getFunctions()), you can see, that the parameters have to be passed like with a normal function call.
So you could do either the verbose thing with SoapParam:
$response = $client->auth(
new SoapParam($request["api_id"], "api_id"),
new SoapParam($request["user"], "user"),
new SoapParam($request["password"], "password")
);
Or less verbose by just giving the parameters directly:
$response = $client->auth($request["api_id"],
$request["user"], $request["password"]);
I am running following code in php:
<?php
$client = new SoapClient('http://devapi.stellatravelgateway.stellatravelservices.co.uk/DirectoryService.svc?wsdl', array('trace' => 1));
$result = $client -> Authenticate( array(
'authenticateRequest' => array('BranchCode' => '6937',
'UserName' => 'xxxx',
'Password' => 'xxxx',
'Application' => 'ExternalAPI',
'BranchID' => '1')));
echo $client->__getLastRequest();
?>
It is resulting this SOAP request
<?xml version="1.0" encoding="UTF-8" ?>
- <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://stellatravelgateway.stellatravelservices.co.uk/DirectoryService">
- <SOAP-ENV:Body>
- <ns1:Authenticate>
- <ns1:authenticateRequest BranchCode="6937" UserName="xxxx" Password="xxxx" Application="ExternalAPI">
<ns1:BranchID>1</ns1:BranchID>
</ns1:authenticateRequest>
</ns1:Authenticate>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
But I want this output...
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://stellatravelgateway.stellatravelservices.co.uk/DirectoryService/IDirectoryService/Authenticate</Action>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Authenticate xmlns="http://stellatravelgateway.stellatravelservices.co.uk/DirectoryService">
<authenticateRequest Password="BREEZE2" Application="ExternalAPI" BranchCode="xxxx" UserName="xxxx">
<BranchID>1</BranchID>
</authenticateRequest>
</Authenticate>
</s:Body>
</s:Envelope>
If you return a content-type that is XML, the UI will depend on your browser.
In IE9 and Firefox, I get it the way you want including the + and - to expand/collapse the tags.
If you want to display this in a webpage with your own format, you will have to parse it yourself.
You can use XML readers.
There are a couple of XML readers on PHP.net, you can pick the one that is the most appropriate for you: http://us.php.net/xml