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
Related
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();
}
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"
Request XML
<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://schemas.navitaire.com/WebServices/ISessionManager/Logon</Action>
<h:ContractVersion xmlns:h="http://schemas.navitaire.com/WebServices">330</h:ContractVersion>
</s:Header>
<s:Body>
<LogonRequest xmlns="http://schemas.navitaire.com/WebServices/ServiceContracts/SessionService">
<logonRequestData xmlns:d4p1="http://schemas.navitaire.com/WebServices/DataContracts/Session" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:DomainCode>WWW</d4p1:DomainCode>
<d4p1:AgentName>API****</d4p1:AgentName>
<d4p1:Password>********</d4p1:Password>
<d4p1:LocationCode i:nil="true" />
<d4p1:RoleCode>APIB</d4p1:RoleCode>
<d4p1:TerminalInfo i:nil="true" />
</logonRequestData>
</LogonRequest>
</s:Body>
</s:Envelope>
The WSDL contains http://pastie.org/9263788
PHP Code
$options = array("soap_version"=> SOAP_1_1,
"trace"=>1,
"exceptions"=>0
);
$client = new SoapClient('https://trtestr3xapi.navitaire.com/sessionmanager.svc?wsdl',$options);
$header[] = new SoapHeader('http://schemas.microsoft.com/ws/2005/05/addressing/none','Action','http://schemas.navitaire.com/WebServices/ISessionManager/Logon',1);
$header[] = new SoapHeader('http://schemas.navitaire.com/WebServices','ContractVersion','330', false);
$client->__setSoapHeaders($header);
$params = array("LogonRequestData" => array("AgentName" => "API*****",
"Password" => "Pass****",
"RoleCode" => "APIB",
"DomainCode" => "WWW"));
try{
$h= $client->Logon($params);
print nl2br(print_r($h, true));
echo 'Request : <br/><xmp>',
$client->__getLastRequest();
echo '</xmp>';
}
catch(SoapFault $fault){
echo 'Request : <br/><xmp>',
$client->__getLastRequest(),
'</xmp><br/><br/> Error Message : <br/>',
$fault->getMessage();
}
XML Request Generated from __getLastRequest()
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.navitaire.com/WebServices/ServiceContracts/SessionService" xmlns:ns2="http://schemas.microsoft.com/ws/2005/05/addressing/none" xmlns:ns3="http://schemas.navitaire.com/WebServices">
<SOAP-ENV:Header>
<ns2:Action SOAP-ENV:mustUnderstand="1">http://schemas.navitaire.com/WebServices/ISessionManager/Logon</ns2:Action>
<ns3:ContractVersion>330</ns3:ContractVersion>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:LogonRequest/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
As you can see the XML Body is empty. How do I flood the body with the parameters?
Full output can bee seen here which is caught in the try{ } block http://pastie.org/9295467
In the output you can also see
//If i use soap version 1.1
[faultstring] => Bad Request
[faultcode] => HTTP
//If i use soap version 1.2
[faultstring] => Cannot process the message because the content type 'application/soap+xml; charset=utf-8; action="http://schemas.navitaire.com/WebServices/ISessionManager/Logon"' was not the expected type 'text/xml; charset=utf-8'.
[faultcode] => HTTP
Did you try switching to SOAP_1_2 because the error message looks like to be associated to this.
Otherwise, you could use a WSDL to php generator such as WsdlToPhp at wsdltophp.com
I had the same problem and manage to fix it using SOAP_1_1 without compression.
So just disable the compression.
Here's the xml structure that I want to achieve:
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" 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" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:method>
<auth>
...
</auth>
<data>
...
</data>
</ns1:method>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</XML>
My code:
<?php
$client = new SoapClient(null, array('location' => "http://www.test.com/soap.php",
'uri' => "http://tempuri.org/",
'trace' => true,'exceptions' => true,'cache_wsdl' => WSDL_CACHE_NONE));
$request = new stdClass();
$request->auth = new stdClass();
$request->data = new stdClass();
$client->__soapCall("method", array($request));
Please note: SoapClient doesn't allow me to insert anything other than array in second param to the __soapCall method.
So here's what I'm getting:
<?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: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" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:method>
<param0 xsi:type="SOAP-ENC:Struct">
<auth xsi:type="SOAP-ENC:Struct"/>
<data xsi:type="SOAP-ENC:Struct"/>
</param0>
</ns1:method>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
As you can see, php added a root tag for my request - param0. Is there a way I can escape this? I need to have 2 "root" elements in that request parameter. How can I achieve that?
did you try this way : $client->__soapCall("method", $request);?
Thanks all for answering. I solved the issue with the help of SoapParam:
$client->__soapCall("method", array(
new SoapParam($request->auth, "auth"),
new SoapParam($request->data, "data"),
));
Can you help me out or point me in the right direction, I'm trying to perform a soap request to a WSDL feed but I'm not getting anything back.
When I use standard XML everything seems to work OK - how would I write the following into an array:
<?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>
<SupplierDirectorySearch xmlns="SOAPREQUEST">
<SupplierSearchDetails ClassVersion="1.0">
<Identification>
<SchemeOperatorRef>59582</SchemeOperatorRef>
<SecurityToken>MYTOKEN</SecurityToken>
</Identification>
<ApprovedServices ServiceRepair="Y" MOT="" Tyres="" CollectionDelivery="" CourtesyCar="" WhileUWait="" Callout24Hour="" BreakdownCover="" CollectionDeliveryNotes="" CourtesyCarNotes="" Inspections=""/>
<SupplierLocation>BB1</SupplierLocation>
<SearchRadiusMiles>300</SearchRadiusMiles>
<Preference>P</Preference>
<MaxReturnNumber>5</MaxReturnNumber>
<PageNo>0</PageNo>
</SupplierSearchDetails>
</SupplierDirectorySearch>
</soap:Body>
</soap:Envelope>';
I've written the following soap request:
$client = new SoapClient("URL?WSDL", $option);
$res = $client->SupplierDirectorySearch(
array('SupplierSearchDetails'=>
array('Identification' => array('SchemeOperatorRef'=>'61', 'SecurityToken'=>'MYTOKEN'),
'ApprovedServices' => array(
'ServiceRepair'=>'Y',
'MOT'=>'',
'Tyres'=>'',
'CollectionDelivery'=>'',
'CourtesyCar'=>'',
'WhileUWait'=>'',
'Callout24Hour'=>'',
'BreakdownCover'=>'',
'CollectionDeliveryNotes'=>'',
'CourtesyCarNotes'=>'',
'Inspections'=>'',
),
'SupplierLocation' => 'BB1',
'SearchRadiusMiles' => '2',
'Preference' => 'P',
'MaxReturnNumber' => '5',
'PageNo' => '0'
)
)
);
Nothing is coming back, I've enabled the trace and this is what's getting passed in the request:
REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="OneLink_ServiceBookingComponent">
<SOAP-ENV:Body><ns1:SupplierDirectorySearch>
<ns1:SupplierSearchDetails>
<ns1:Identification>
<ns1:SchemeOperatorRef>61</ns1:SchemeOperatorRef>
<ns1:SecurityToken>MYTOKEN</ns1:SecurityToken>
</ns1:Identification>
<ns1:SupplierLocation>BB1</ns1:SupplierLocation>
<ns1:SearchRadiusMiles>2</ns1:SearchRadiusMiles>
<ns1:Preference>P</ns1:Preference>
<ns1:ApprovedServices ServiceRepair="Y" MOT="" Tyres="" CollectionDelivery="" CourtesyCar="" WhileUWait="" Callout24Hour="" BreakdownCover="" CollectionDeliveryNotes="" CourtesyCarNotes="" Inspections=""/>
<ns1:MaxReturnNumber>5</ns1:MaxReturnNumber>
<ns1:PageNo>0</ns1:PageNo>
</ns1:SupplierSearchDetails>
</ns1:SupplierDirectorySearch>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Can anyone shed any light on this, it's racking my brain!!
I've just been looking, do you think I will have to code the section like this:
<?php
$amount['_'] = 25;
$amount['currencyId'] = 'GBP';
$encodded = new SoapVar($amount, SOAP_ENC_OBJECT);
?>
Scott