PHP based SOAP headers how to generate Specific format ? - php

I wanted to generate the following SOAP header format,
<soapenv:Header>
<SoapHeaderMsg xmlns="http://xyz.com.au">
<opt:UserSoapHeader>
<opt:IdentityName>TEST</opt:IdentityName>
<opt:AuthenticationToken>jjjkjkjkjkjkj</opt:AuthenticationToken>
</opt:UserSoapHeader>
</SoapHeaderMsg>
</soapenv:Header>
So i am using the following php functions to generate this,
$this->__setSoapHeaders(array(
new SoapHeader('http://xyz.com.au', 'SoapHeaderMsg', array(
new SoapHeader('http://xyz.com.au', 'IdentityName', 'TEST'),
new SoapHeader('http://xyz.com.au', 'AuthenticationToken', 'jkjkjkk')
)),
));
Which generates following headers which is completely different to what i wanted above ? how do i generate exact same headers using PHP functions like above ?
<SOAP-ENV:Header>
<ns1:SoapHeaderMsg>
<SOAP-ENC:Struct>
<namespace>http://xyz.com.au</namespace>
<name>IdentityName</name>
<data>TEST</data>
<mustUnderstand>false</mustUnderstand>
</SOAP-ENC:Struct>
<SOAP-ENC:Struct>
<namespace>http://xyz.com.au</namespace>
<name>AuthenticationToken</name>
<data>hjhhjjhjhjhj</data>
<mustUnderstand>false</mustUnderstand>
</SOAP-ENC:Struct>
</ns1:SoapHeaderMsg>
</SOAP-ENV:Header>

array is mostly used on the PHP side, which gets converted to Struct. Can you please try to use object and see if you have any success with it.
$this->__setSoapHeaders(array(
new SoapHeader('http://xyz.com.au', 'SoapHeaderMsg',
(object)array(
'opt:UserSoapHeader' => (object)array(
'opt:IdentityName' => 'TEST',
'opt:AuthenticationToken' => 'jkjkjkk'
)
)),
));
The request looks like below:
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://test.domain.com/"
xmlns:ns2="http://xyz.com.au">
<SOAP-ENV:Header>
<ns2:SoapHeaderMsg>
<opt:UserSoapHeader>
<opt:IdentityName>TEST</opt:IdentityName>
<opt:AuthenticationToken>jkjkjkk</opt:AuthenticationToken>
</opt:UserSoapHeader>
</ns2:SoapHeaderMsg>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
......
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Related

PHP soap xml vs SoapUI xml

I have a working SOAP UI xml, and I have my SOAP request XML and they are almost identical. SOAP UI works, mine gets a null response. Here's the SOAPUI XML first
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:get="http://www.cornerstoneondemand.com/Webservices/GetDetailsWebService">
<soapenv:Header>
<get:AuthHeader>
<get:CorpName>corp</get:CorpName>
<get:UserId>1234</get:UserId>
<get:Signature>ABC123</get:Signature>
</get:AuthHeader>
</soapenv:Header>
<soapenv:Body>
<get:GetDetails xmlns:get="http://www.cornerstoneondemand.com/Webservices/GetDetailsWebService">
<get:object_id>qwerty-123</get:object_id>
</get:GetDetails>
</soapenv:Body>
</soapenv:Envelope>
and here is my PHP code and the request.
$client=new SoapClient($wsdl,array('trace' => 1, 'exception' => 0));
$auth = array(
'CorpName' => $CorpName,
'UserId' => $username,
'Signature' => $Signature
);
$header = new SoapHeader('http://www.cornerstoneondemand.com/Webservices/GetDetailsWebService','AuthHeader',$auth,false);
$client->__setSoapHeaders($header);
$parm[] = new SoapVar($LOid, XSD_STRING, null, null, 'object_id' );
var_dump($client->GetDetails( new SoapVar($parm, SOAP_ENC_OBJECT) )); //output is NULL
//and the PHP request:
print_r($client->__getLastRequest());
output is
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.cornerstoneondemand.com/Webservices/GetDetailsWebService">
<SOAP-ENV:Header>
<ns1:AuthHeader>
<ns1:CorpName>corp</ns1:CorpName>
<ns1:UserId>1234</ns1:UserId>
<ns1:Signature>ABC123</ns1:Signature>
</ns1:AuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetDetails>
<object_id>qwerty-123</object_id>
</ns1:GetDetails>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I can't tell if I'm close to creating a good request, or miles off. I'm working to make the PHP request match SOAPUI's since it works and mine doesn't.
The contain the nearly same information. Namespace prefixes for element nodes are exchangeable and optional. So all these 3 variants are resolved to and element node with the local name Envelope in the namespace http://schemas.xmlsoap.org/soap/envelope/.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"/>
You can read the element name as {http://schemas.xmlsoap.org/soap/envelope/}:Envelope.
The same goes for the get vs ns1 namespaces prefixes. They both resolve to the same actual namespace.
But the element object_id has no namespace in your XML. The sixth argument of the SoapVar constructor is the node namespace so you might want to try:
$namespace = 'http://www.cornerstoneondemand.com/Webservices/GetDetailsWebService';
$parm[] = new SoapVar($LOid, XSD_STRING, null, null, 'object_id', $namespace);

Unable to convert soap responce to json format in php

I am using one payment gateway that return response as xml and I am trying to convert to json buy its not converting. I have tried that simplexamlloadfile , simplexmlsting function and etc.
But its not working.
Code :
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<ns2:SingleVaultResponse xmlns:ns2="http://www.paygate.co.za/PayHOST">
<ns2:LookUpVaultResponse>
<ns2:Status>
<ns2:StatusName>Completed</ns2:StatusName>
<ns2:PayVaultData>
<ns2:name>cardNumber</ns2:name>
<ns2:value>411111xxxxx1111</ns2:value>
</ns2:PayVaultData>
<ns2:PayVaultData>
<ns2:name>expDate</ns2:name>
<ns2:value>012025</ns2:value>
</ns2:PayVaultData>
<ns2:PaymentType>
<ns2:Method>CC</ns2:Method>
<ns2:Detail>Visa</ns2:Detail>
</ns2:PaymentType>
</ns2:Status>
</ns2:LookUpVaultResponse>
</ns2:SingleVaultResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Try this:
$string2 = 'your xml sting';
$doc = new DOMDocument;
$doc->loadXML($string2);
echo "status => ".$doc->getElementsByTagName('Status')->item(0)->nodeValue;

How to work with SOAP web service and it's XML in php

I want to call a function of a web service in my project developed by PHP. This is my code:
$wsdl="http://x.x.x.x:8090/charge/services/Amount?wsdl";
$client = new SoapClient($wsdl) or die("Error");
$chargeAmountArray = array('UserIdentifier' => $number ,
'data' => array(
'description' => array("Channel=test|".$Origin),
'currency' => NULL,
'code' => $code),
'refrence' => $refcode);
$header = new SoapHeader('servicekey', $servicekey);
$client->__setSoapHeaders($header);
$res = $client->__call('chargeAmount',$chargeAmountArray);
return $res->return;
I have seen my xml code with tcpdump and it is like:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.csapi.org/schema/parlayx/payment/amount_charging/v4_0/local" xmlns:ns2="servicekey">
<SOAP-ENV:Header>
<ns2:e0ce5ed56d7c4d60/>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:chargeAmount/>
<param1>
<item>
<key>description</key>
<value>yChannel=test|30733</value>
</item>
<item>
<key>currency</key>
<value/>
</item>
<item>
<key>code</key>
<value>MOBDY</value>
</item>
</param1>
<param2>1PN1mROoZop2hAy</param2>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
As you see the UserIdentifier is not passed and the refrence parameter has no name in the XML.
I see a few errors here.
The constructor signature for the SoapHeader class shows the first parameter is a namespace declaration, which it doesn't appear you are setting.
Also, SoapClient::__call() is deprecated; instead you should call the method directly:
$res = $client->chargeAmount($chargeAmountArray);
SoapClient::__call() expects parameters in an array with key "parameters", which you are not providing.
Finally, you should be doing some error checking with try/catch in case the connection can't be made for whatever reason.

Transform key-value XML into other format with PHP SOAPclient?

I want to transform my SOAP request from key-value format to another:
<SOAP-ENV:Header>
<ns2:AUTH>
<item>
<key>username</key>
<value>testuser</value>
</item>
<item>
<key>password</key>
<value>xxxxx</value>
</item>
</ns2:AUTH>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:getProductConfig/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
This is what I want:
<SOAP-ENV:Header>
<ns2:AUTH>
<username>testuser</username>
<password>xxxxx</password>
</ns2:AUTH>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:getProductConfig>
<partner_id>1</partner_id>
</ns1:getProductConfig>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
This is my PHP code where I create a new SoapClient object:
$header = new SoapHeader('chainstore-api','AUTH',$headerOptions, FALSE);
$req = new SoapClient(NULL, $options);
$req->__setSoapHeaders($header);
$res = $req->getProductConfig($params);
echo $res;
PS: Currently, It's non wsdl style but I'm thinking about to change to use wsdl too.
This may sound crazy, but I just fixed this by using WSDL instead.
It is much easier. WSDL file will determine both SOAP request and response.

Nusoap change returnname

I'd like to change the tag that is currently 'return' to 'result'. Like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
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:Body>
<ns1:AuthenticateResponse xmlns:ns1="http://tempuri.org">
<***return***>
<Result xsi:type="xsd:string">OK</Result>
</***return***>
</ns1:AuthenticateResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
(see the value between the ***)
Is this possible? And if so, how?
I've fixed this myself by changing the nusoap.php file (line 4143 like this:
original:
$return_val = $this->serialize_val($this->methodreturn, 'return');
Edited code:
$return_val = $this->serialize_val($this->methodreturn, 'result');
I bet there is a better way of handling this, but this fixes it for me.

Categories