PHP - How to set footer in SoapClient - php

With PHP, I need to send a SOAP request with a parameter (Hash) in the footer. I'm using SoapClient but I can not figure out how to do this, neither in internet searches, nor in documentation.
This is the envelope I used in the SoapUI tool to test:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:abc="" xmlns:abc1="" xmlns:abc2="">
<soapenv:Header/>
<soapenv:Body>
<abc:Method>
<!--Optional:-->
<abc:request>
<abc1:Header>
<abc1:Username>Username</abc1:Username>
<abc1:Password>Password</abc1:Password>
<!--Optional:-->
<abc1:PublicKeyUid></abc1:PublicKeyUid>
</abc1:Header>
<abc1:Body>
<abc2:Id></abc2:Id>
</abc1:Body>
<abc1:Footer>
<abc1:Hash></abc1:Hash>
</abc1:Footer>
</abc:request>
</abc:Method>
</soapenv:Body>
</soapenv:Envelope>
There is The SoapHeader class e the SoapClient::__setSoapHeaders method but I find nothing related to the footer.
I do not have access to the server and should follow this structure mentioned above.
What I need to know is how to send the HASH parameter that is inside the footer with SoapClient.
Thanks in advance for any help or suggestion.

As IMSoP and axiac have commented, the footer (<abc1: Footer>) is just a sub-structure of the envelop body and not a standard XML envelop element. I had not realized this and so I did not find it in the documentation.
So to send HASH to the requested structure, I need to pass a multidimensional array on the body element. The code looks like this:
$arrParameters = [
'request'=>[
'Header'=>[
'Username'=>$strUsername,
'Password'=>$strPassword
],
'Body'=>[
...
],
'Footer'=>[
'Hash'=>$strHash
]
]
];
$SoapClient = new SoapClient(<WSDL URL>);
$resp = $SoapClient->method($arrParameters);
Thank you IMSoP and axiac!

Related

How can I format a PHP array that uses elements and attributes?

I am trying to send a SOAP request via PHP and while I am able to send via SOAPUI, I am having an issue formatting in PHP. The schema, located here shows an attribute embedded in the routePartitionName element which is where my code is falling apart. I need to send the pattern and routePartitionName in my array. Here is part of the SOAP request that is working:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/12.5">
<soapenv:Header/>
<soapenv:Body>
<ns:getLine sequence="?">
<pattern>6304389152</pattern>
<routePartitionName uuid="{BE888889-7C30-4C89-0CC4 C99FDE1025A9}">NA_DID_XLATE_PT</routePartitionName>
<returnedTags uuid="?">
.....
.....
</returnedTags>
Here is what I am sending in PHP that does not work:
$lineinfo = array("routePartitionName"=>"uuid={BE888889-7C30-4C89-0CC4-C99FDE1025A9}",
"pattern"=>"6304389152");
The logs show `[soapenv:Server] Item not valid: The specified uuid={BE888889-7C30-4C89-0CC4-C99FDE1025A9} was not found `
Can someone let me know what I am missing here?
Thanks.

SOAP request and repsonse with PHP (display response)

I need to make a SOAP request and get a response back, but I have no idea how SOAP works. I tried to search for it and everything is so confusing.
I need to make an authentication request here like this :
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dir="http://stellatravelgateway.stellatravelservices.co.uk/DirectoryService">
<soapenv:Header/>
<soapenv:Body>
<dir:Authenticate>
<!-- Optional: -->
<dir:authenticateRequest BranchCode="abcde" UserName="user" Password="password" Application="application" Client="?">
<dir:BranchID>1</dir:BranchID>
</dir:authenticateRequest>
</dir:Authenticate>
</soapenv:Body>
</soapenv:Envelope>
And get an response after that, but have no idea how to do it. I searched and found some similar questions, but can not get any response.
What I am doing is this :
$send = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dir="http://stellatravelgateway.stellatravelservices.co.uk/DirectoryService">
<soapenv:Header/>
<soapenv:Body>
<dir:Authenticate>
<!-- Optional: -->
<dir:authenticateRequest BranchCode="abcde" UserName="user" Password="password" Application="application" Client="?">
<dir:BranchID>1</dir:BranchID>
</dir:authenticateRequest>
</dir:Authenticate>
</soapenv:Body>
</soapenv:Envelope>';
$soapUrl ="http://devapi.stellatravelgateway.stellatravelservices.co.uk/DirectoryService.svc?singleWsdl";
$soapClientVar = new SoapClient("http://devapi.stellatravelgateway.stellatravelservices.co.uk/DirectoryService.svc?singleWsdl");
$resp = $soapClientVar->Authenticate($send);
var_dump($resp);
I know that 99% I totally wrong of what should I do. Can someone please help me understand what exactly should I do and make this SOAP work?
TIA
The WSDL sets up the SoapClient:
http://devapi.stellatravelgateway.stellatravelservices.co.uk/DirectoryService.svc?singleWsdl
and tells the client what to expect in terms of methods to call on the SOAP service. SoapClient takes care of creating what you see in $send.
Instead of sending raw SOAP (which SoapClient will do for you), you work at the method level. From the WSDL the Authenticate() method takes a parameter of type tns:AuthenticateRequest which contains BranchCode, UserName etc. and returns an object of type tns:AuthenticateResponse, containing tns:ResultBase which contains the actual result Success, Narrative etc.
this might get you towards your solution:
$soapClientVar = new SoapClient("http://devapi.stellatravelgateway.stellatravelservices.co.uk/DirectoryService.svc?singleWsdl");
$params = array(
"BranchCode" => $BranchCode,
"UserName" => $UserName,
"Password" => $Password,
"Application" => $Application,
"Client" => $Client
);
$response = $soapClientVar->__soapCall("Authenticate", array($params));

Calling multiple methods with same name using SoapClient

I have a SOAP webservice and in SOAP UI I see that there are methods with the same name. So, for example, there are 2 CreateNewContact methods, one of which takes 3 parameters and the other 4. Below are the stubs generated by SOAP UI
Method 1 Stub:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rfp="http://test.com/testWebservice/">
<soapenv:Header/>
<soapenv:Body>
<rfp:CreateNewContact_FullName>
<!--Optional:-->
<rfp:fullName>?</rfp:fullName>
<!--Optional:-->
<rfp:email>?</rfp:email>
<!--Optional:-->
<rfp:telNo>?</rfp:telNo>
</rfp:CreateNewContact_FullName>
</soapenv:Body>
</soapenv:Envelope>
Method 2 Stub:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rfp="http://test.com/testWebservice/">
<soapenv:Header/>
<soapenv:Body>
<rfp:CreateNewContact_FirstLastName>
<!--Optional:-->
<rfp:firstName>?</rfp:firstName>
<!--Optional:-->
<rfp:lastName>?</rfp:lastName>
<!--Optional:-->
<rfp:email>?</rfp:email>
<!--Optional:-->
<rfp:telNo>?</rfp:telNo>
</rfp:CreateNewContact_FirstLastName>
</soapenv:Body>
</soapenv:Envelope>
When I call the CreateNewContact method with 4 parameters using PHP SoapClient, it looks like I'm getting the response from the other method.
How can I specify which method to use using SoapClient?
Thanks,
As you can read here:
If you are using WSDL based SOAP requests and you have more than one
operation in your binding (with the same parameters), make sure the
style is set to rpc, NOT body! When you specify
'body' here, all that will be transmitted in the request is the
parameters for the function call, and SoapServer->handle() will use
the first function it finds with the same parameter-makeup to handle
the call. The actual method to call will only be included in the
request when your type is set to 'rpc', resulting in the expected
behavior
Therefore, you should check in your WSDL the operation element, which provides binding information from the abstract operation to the concrete SOAP operation.
For example:
<definitions ....>;
<binding .... >;
<operation .... >;
<soap12:operation soapAction="xs:anyURI" ?
soapActionRequired="xs:boolean" ?
style="rpc|document" ?
wsdl:required="xs:boolean" ? /> ?
</soap12:operation>
</binding>;
</definitions>
The style attribute value, if present, is a string that specifies the style for the operation. The style attribute indicates whether the operation is RPC-oriented (a messages containing parameters and return values) or document-oriented (a message containing documents). If the style attribute is omitted from the soap12:operation element, then the operation inherits the style specified or implied by the soap12:binding element in the containing wsdl:binding element.
So, in short, to solve your problem you should change the operation style from "document" to "rpc" in your WSDL.
As a further reference: https://bugs.php.net/bug.php?id=49169
I have faced the same with travelport universal API, I ended up modifying my local wsdl file to use different name for each method and it worked perfectly.

Generating schema with soap PHP client

How can I generate this schema using php soap client cant seem to get a way
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:loc="http://www.csapi.org/schema/parlayx/subscribe/manage/v1_0/local">
<soapenv:Header>
<tns:RequestSOAPHeader xmlns:tns="http://www.namesapace.com/common/v2_1">
<tns:one>***</tns:one>
<tns:two>***</tns:three>
<oauth_token>***</oauth_token>
</tns:RequestSOAPHeader>
</soapenv:Header>
<soapenv:Body>
<loc:ProductRequest>
<subInfo>
<productID>***</productID>
<isAutoExtend>0</isAutoExtend>
</subInfo>
</loc:ProductRequest>
</soapenv:Body>
</soapenv:Envelope>
On top of using the client, sometimes it may not bind to the namespace especially if using different soap versions or other strange reasons. Solved it by overriding the soap class and replacing the ns1 and ns2 values as described here http://php.net/manual/en/soapclient.dorequest.php#74123
IF no success with php soapclient, just post the request with curl.
Example in this answer - SOAP request in PHP with CURL

Passing Arrays as Parameters to Soap Webservice in non-WSDL mode

I am using Zend_Soap_Client to query data from a webservice provided by SAP. Since the auto-generated WSDL file has a few flaws, I use the non-WSDL mode of the client.
I managed to successfully call a webservice which only requires simple parameters, like strings. Example:
This is what SAP expects:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style">
<soapenv:Header/>
<soapenv:Body>
<urn:Ze12RfcGetCustHistoryNew>
<PiDateHigh>2011-12-31</PiDateHigh>
<PiDateLow>1970-01-01</PiDateLow>
<PiKunnr>1</PiKunnr>
</urn:Ze12RfcGetCustHistoryNew>
</soapenv:Body>
</soapenv:Envelope>
This is my (working) code in PHP (with $soapClient already initialized in non-WSDL mode):
$soapClient->Ze12RfcGetCustHistoryNew(
new SoapParam(date('Y-m-d'), 'PiDateHigh'),
new SoapParam('1970-01-01', 'PiDateLow'),
new SoapParam('1', 'PiKunnr')
);
But as soon as I have to pass more complex parameters to the service, it does not work. Again, an example:
This is what SAP expects:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style">
<soapenv:Header/>
<soapenv:Body>
<urn:Ze12RfcGetCustHistoryNew>
<PiDateHigh>2011-12-31</PiDateHigh>
<PiDateLow>1970-01-01</PiDateLow>
<PiKunnr>1</PiKunnr>
<PiTBelegart>
<item>
<BelegartTyp>FAKTURA</BelegartTyp>
<Belegart>ZF2</Belegart>
</item>
</PiTBelegart>
</urn:Ze12RfcGetCustHistoryNew>
</soapenv:Body>
</soapenv:Envelope>
I have tried to use a multi-dimensional array containing SoapParams, but that did not work. In WSDL mode, I could pass the params as an array, without the need of using SoapParams. How can I do this in non-WSDL mode?
just a "quick-hit" ... I'm working in a different environment, but initially had my soap-value troubles too.
One solution for a particular problem was to pass complex arrays this way:
$data = (object)$complexArray;
$result = $webserviceClient->getResult($data);
"Casting" to object results in a StdClass object ... which often works fine for webservices.
Good Luck!
I have not come up with a nice solution for this yet - currently I pass the parameters to the client object as raw xml. That works, but does not seem to be the best way to do this. This is my code now:
$params = '
<PiDateHigh>2011-12-31</PiDateHigh>
<PiDateLow>1970-01-01</PiDateLow>
<PiKunnr>1</PiKunnr>
<PiTBelegart>
<item>
<BelegartTyp>FAKTURA</BelegartTyp>
<Belegart>ZF2</Belegart>
</item>
</PiTBelegart>
';
$result = $this->_client->Ze12RfcGetCustHistoryNew(new SoapVar($params,XSD_ANYXML));

Categories