Adding optional parameters in SOAP call in PHP - php

The format of my SOAP call needs to be:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body>
<ns1:FunctionName>
<ns1:HostUserName>hostuser</ns1:HostUserName>
<ns1:HostPassword>pass</ns1:HostPassword>
<ns1:OwnerName>owner</ns1:OwnerName>
<ns1:UserName>user</ns1:UserName>
<ns1:OptionalParam>
<ns1:Parameters>
<ns1:Parameter Name="FirstName">First<ns1:/Parameter>
<ns1:Parameter Name="LastName">Last<ns1:/Parameter>
<ns1:/Parameters>
</ns1:OptionalParam>
<ns1:/FunctionName>
</SOAP-ENV:Body>
How do I represent this in a PHP request?
I build the request like this:
$client = new SoapClient($url,array('trace' => 1));
$data['HostUserName']=$this->hostname;
$data['HostPassword']=$this->hostpassword;
$data['OwnerName']=$this->ownername;
$data['UserName']=$username;
$data['FirstName']=$FirstName;
$data['LastName']=$LastName;
$result = $client->FunctionName($data);
I've tried a few combinations of building arrays, SOAPVAR and SOAPPARAM but nothing has worked.
The target SOAP server is .NET based.
Any suggestions?
Thanks

Related

PHP SOAP request fails

I have to send a request to a remote SOAP endpoint. This is the SOAP request that PHP generates, and it fails:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org">
<SOAP-ENV:Body>
<ns1:GetParameterList>
<param>TKERES</param>
</ns1:GetParameterList>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
If I write the request XML manually this way and send it via cURL, it works perfectly:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<GetParameterList xmlns="http://tempuri.org">
<param>TKERES</param>
</GetParameterList>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Why do the first fail while the second one works correctly?
What is obvious here is that your first snippet (the one that doesn't work) is different from the one that works.
Specifically the property below looks to be in totally different places.
xmlns="http://tempuri.org"
I know from experience with SOAP that sometimes a change like this can cause it to fail. So, try if you can to adjust the way that PHP generates the request in order to match it with your XML hand written one.

Stuck at converting xml request to corresponding Savon request

I am trying to communicate with a 3-rd party API. I tried both savon and Soap UI but both failed, then I asked them and they gave me equivalent that works. Confused, I toggled the logger and got following XML request which is quite different from what Savon is sending.
Here is the PHP code (I guess that's not needed, but still)
$apiauth =array('UserName'=>'XXXXXX','Password'=>'XXXXXXX','ClientCode'=>'1234')
$wsdl = 'http://stagewebapi.mylescars.com/service.asmx?wsdl';
$soap = new SoapClient($wsdl);
$header = new SoapHeader('http://tempuri.org/', 'AuthHeader', $apiauth);
$soap->__setSoapHeaders($header);
$data = $soap->fetchCities($header);
print_r($data);
Here is the XML request that PHP is sending and which is working just fine
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/"><SOAP-ENV:Header><ns1:AuthHeader><ns1:UserName>XXXXXXX</ns1:UserName><ns1:Password>XXXXXXX</ns1:Password><ns1:ClientCode>1234</ns1:ClientCode></ns1:AuthHeader></SOAP-ENV:Header><SOAP-ENV:Body><ns1:fetchCities/></SOAP-ENV:Body></SOAP-ENV:Envelope>
Here is my ruby code so far
require 'savon'
auth_header = { 'UserName'=>'XXXXXXX','Password'=>'XXXXXXXX','ClientCode'=>'1234'}
client = Savon.client(:wsdl => "http://stagewebapi.mylescars.com/service.asmx?wsdl",
:namespace_identifier => :ns1 ,:namespace => "http://tempuri.org/" , :log => true, :soap_header => auth_header,
:pretty_print_xml => true, :env_namespace => 'SOAP-ENV')
puts client.call(:fetch_cities)
Here is what Savon is producing
<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://tempuri.org/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header>
<UserName>XXXXXXXXXXXXXXX</UserName>
<Password>XXXXXXXXXXXXX</Password>
<ClientCode>2873</ClientCode>
</soap-env:Header>
<soap-env:Body>
<ns1:fetchCities/>
</soap-env:Body>
</soap-env:Envelope>
I don't know how to make it equivalent to the PHP one. Am I missing something obvious ?
I tried to look at Savon documentation to find if there are some methods to manipulate tags with fine grain control but couldn't find much about it.
Questions similar to this are either unanswered or are specific to the requests.
Some SO questions that helped a little
Convert this xml request to a proper savon request
Replicating xml requests with savon ruby
Little curious, how PHP gets it right most of the time. Is it the parser they use for wsdl?

php and soap headers token auth

I need this xml in out:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="https://server/">
<soapenv:Header>
<authenticate>111111111111</authenticate>
<SOAP-ENV:Header/>
My php code is:
$head = new stdClass();
$head->authenticate='1111111111111111';
$header = new SoapHeader('ns1','authenticate',$head,false);
$client = new SoapClient ('https://server',array("trace" => 1, "exceptions" => 1));
$client->__setSoapHeaders($header);
I get this XML:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="https://server"
xmlns:ns2="https://server">
<SOAP-ENV:Header>
<ns2:authenticate>
<authenticate><BOGUS>anc401t9n4dknmgp2bv629ori7</BOGUS>
</authenticate></ns2:authenticate>
</SOAP-ENV:Header
><SOAP-ENV:Body>...
How can I get needed xml? Can fully rewrite soapheader with php client SoapClient? Or how I can adding one string whithout creating new namespace?
If your Web Service is well documented within its WSDL, try using wsdltophp.com or PackageGenerator as it generates the methods to properly set the SoapHeader very easily and so for the whole request.

Using PHP SOAP client on .NET web service

I am trying to consume a .NET SOAP service but I'm currently just getting a 'false' response.
Visiting the service endpoint tells me the following:
The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SendData xmlns="http://tempuri.org/">
<myObj>string</myObj>
</SendData>
</soap:Body>
</soap:Envelope>
But when I check my request, it is sending the following:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body>
<ns1:SendData>
<ns1:myObj>My data</ns1:myObj>
</ns1:SendData>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
So we can see that my request has different nodes titles and also adds in "ns1:" to the parameters.
Here is my (brief) PHP
$soap = new SoapClient('wsdl.xml', array("trace" => 1, "exception" => 1));
$call = $soap->__soapCall("SendData", array("SendData" => array("myObj" => "My data")));
So my question is: Could this difference in request schema be responsible for the request failing or is this just another way of writing the same thing? If it is responsible, is it possible to write my request exactly as specified at the endpoint?
It is the same thing.
In the given example <SendData xmlns="http://tempuri.org/"> is without namespace prefix (the ns1 in your own request) so default prefix is used, and it specifies it with xmlns attribute for itself and its descendants.
In your request ns1 namespace prefix is defined using xmlns:ns1="http://tempuri.org/" and used in <ns1:SendData>.

PHP SOAP Request Attributes not created properly

I am trying to create a SOAP request in PHP using NUSOAP,
require_once('lib/nusoap.php');
$client = new nusoap_client('wsdl_link', true);
The structure of the request i want to generate is:
<rootlevel att1="abc" att2="def" >
<Start>
</Start>
</rootlevel >
The parameters are as follows:
$params=array('att1'=>'abc',
'att2'=>'def',
'start'=>array(/*array defined here*/));
$result = $client->call('function_name', array('rootlevel' => $params));
But with the request generated is as follows:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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/" xmlns:ns4439="http://tempuri.org">
<SOAP-ENV:Body>
<rootlevel xmlns="http://site/01">
<start>
</start>
</rootlevel>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Kindly tel me why my attributes are getting replaced by xmlns..
Edit:
I was going through my wsdl:
In the wsdl, the tag rootlevel is defined as ref="tns:rootlevel" and attributes are defined under name=rootlevel which is somewhere else in the wsdl.
For some of the attributes the request is coming proper(here the tag is defined as name and the attributes are under this tag.). but some are being replaced by xmlns.. Please let me know where im going wrong.
I solved my problem using the "send" function of NUSOAP.. Here u dont have to create an array and all.. The request can be sent in the XML format itself as follows:
$params="<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/" xmlns:ns4439="http://tempuri.org">
<SOAP-ENV:Body>
<rootlevel xmlns="http://site/01">
<start>
</start>
</rootlevel>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>";
$result = $client->send($params, 'wsdl_url/function',0,$timeout);
Hope this Helps You.. :)

Categories