How to form properly __soapCall parameters - php

Trying to make a soap call with parameters:
$client = new SoapClient( null, [
'location' => $url,
'uri' => $uri,
'trace' => 1,
'exceptions' => 1
] );
$params['parameters'] = [
'p1'=>'v1',
'p2'=>'v2'
];
$client->__soapCall('generate',$params);
I want my request look like this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="some_url_here">
<SOAP-ENV:Body>
<ns1:generate>
<parameters>
<p1>v1</p1>
<p2>v2</p2>
</parameters>
</ns1:generate>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
But what I get is:
var_dump($client->__getLastRequest());
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="some_url_here"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:generate>
<param0 xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">p1</key>
<value xsi:type="xsd:string">v1</value>
</item>
<item>
<key xsi:type="xsd:string">p2</key>
<value xsi:type="xsd:string">v2</value>
</item>
</param0>
</ns1:generate>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Any ideas how properly form params list to get wanted request?
Your help would be appreciated.

The best and fast way to consume a Soap Web Service is to use a WSDL to php generator as you won't wonder how to construct the request.
Try the PackageGenerator project and you'll see it's easy to construct the request (without making any error unless you're doing it purposely ;)). In addition, the received response is easily handled. Each part is an object.

Related

PHP SoapServer - Change SOAP-ENV to soapenv

I am creating a SoapServer side in php. Someone makes a request and the server responds with an xml.
I've been asked to change the response envelope from SOAP-ENV to soapenv.
This is how it's looking now:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://testing.bonusfarma.com.ar/api/serverSideSoap" 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:Envelope>
This is how it has to be:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://testing.bonusfarma.com.ar/api/serverSideSoap" 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:Envelope>
I've been trying to change the soap version because I read that it is what defines the envelope but it doesn't seem to work. The change is supposed to be just a parameter:
$server = new SoapServer(null, array(
'location' => 'xxxxx'
'uri' => 'xxxxx'
'soap_version' => SOAP_1_2 // not working
)
);
Have any idea about what's generating the SOAP-ENV and what can I do to change it?

Parse 2 difference Soap Response PHP

I'm new in php, how to parse 2 difference response from soap xml ?
Response 1
'<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 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" xmlns:MobileAgentAPI="urn:openApi">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<MobileAgentAPI:invokeResponse>
<SOAP-ENV:BodySOAP-ENV:Body>
<values xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="MobileAgentAPI:SoapMapValue[13]">
<item>
<name>amount</name>
<singleValue>5000</singleValue>
</item>
<item>
<name>balance</name>
<singleValue>12210000</singleValue>
</item>
<item>
<name>lastBalance</name>
<singleValue>12215000</singleValue>
</item>
<item>
<name>returnCode</name>
<singleValue>00</singleValue>
</item>
<item>
Response 2
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 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" xmlns:MobileAgentAPI="urn:openApi"><SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><MobileAgentAPI:invokeResponse><invokeReturn><values xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="MobileAgentAPI:SoapMapValue[6]"><item><name>balance</name><singleValue>15420000</singleValue></item><item><name>returnCode</name><singleValue>01</singleValue></item><item><name>transactionStatus</name><singleValue>02</singleValue></item><item><name>errorCode</name><singleValue>324</singleValue></item><item><name>errorDescription</name><singleValue>(324) Maaf, transaksi pada 01/10/18 06:00 gagal. Nomor tujuan tidak terdaftar. =</singleValue></item><item><name>tran
im try with DOM Document but if we have 2 response the result is error
Why do you try to parse the XML response?
With the native PHP SoapClient class, you handle at least PHP arrays or betterly objects.
Use a WSDL to PHP generator so you won't wonder how to construct the request data nor how to handle the response as you'll always use an OOP approach which is better. Try the PackageGenerator project.

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.. :)

Problems with C# .NET client using external PHP SOAP web service which returns hash arrays

I am writing a .NET web application that needs to call external webservices. The documentation I have been provided with includes code examples in PHP.
I can successfully create a web reference in VS2010 using the WSDL address that has been provided to me, and using fiddler I can see that the expected XML is getting sent and received. However .NET appears to be having trouble parsing the returned XML.
The simplest web service I'm dealing with just accepts an array of usernames and is meant to return some nested hash arrays of users (with each user it's own array name, type, etc. fields) and an array of errors (for any usernames that didn't match up). The documentation I have describes it in 'PHP-ish':
array (
'users' => array (
array(
'id' => 11,
'username' => 'mick',
'firstname' => 'Mick',
'lastname' => 'Byrne'
),
...
)
'errors' => array(
array(
'username' => 'whoever',
'errorcode' => 'NOSUCHUSER'
)
)
)
I'm getting the SOAP XML that would correspond to this. However, when .NET tries to turn it into a result it throws an exception:
Cannot assign object of type System.Xml.XmlNode[] to an object of type System.String.
Interestingly, the corresponding method that .NET has created for me based on the WSDL says it returns a plain old string which suggests that it can't handle the way the WSDL defines the return type.
The full WSDL is available here:
http://www.elearning.psychology.org.au/webservice/soap/server.php?wsdl=1&wstoken=dc45858adb6f28b7feae87014d46d9b3
Here is a sample of the sent and returned XML from the this basic Get Usernames request:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://www.elearning.psychology.org.au/webservice/soap/server.php?wstoken=dc45858adb6f28b7feae87014d46d9b3" xmlns:types="http://www.elearning.psychology.org.au/webservice/soap/server.php?wstoken=dc45858adb6f28b7feae87014d46d9b3/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:netspot_user_get_users_by_username>
<usernames href="#id1" />
</tns:netspot_user_get_users_by_username>
<soapenc:Array id="id1" soapenc:arrayType="xsd:string[1]">
<Item>557788</Item>
</soapenc:Array>
</soap:Body>
</soap:Envelope>
And response:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.elearning.psychology.org.au/webservice/soap/server.php?wstoken=dc45858adb6f28b7feae87014d46d9b3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:netspot_user_get_users_by_usernameResponse>
<return xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">errors</key>
<value SOAP-ENC:arrayType="ns2:Map[1]" xsi:type="SOAP-ENC:Array">
<item xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">username</key>
<value xsi:type="xsd:string">557788</value>
</item>
<item>
<key xsi:type="xsd:string">errorcode</key>
<value xsi:type="xsd:string">NOSUCHUSER</value>
</item>
</item>
</value>
</item>
</return>
</ns1:netspot_user_get_users_by_usernameResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Any help would be greatly appreciated.
Had the same problem. All I had to do is fix every namespace from https to http in generated cs file. So, it could be your namespaces are wrong.

Barnes and Noble seller SOAP API using PHP

I am new to SOAP and have been trying to connect to Barnes and Noble SOAP API using the php5 built in soap functions.
http://www.php.net/manual/en/class.soapclient.php
My question is, does anyone have any documentation or experience using Barnes and Noble system? I have been going back and forth with the support person and I feel like they assume we should just be able to figure it out.
The faultcode i am getting is "HTTP" and fault string is "Method Not Allowed".
Here is what the support guy says my header should look like.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<SessionInfo xmlns="http://tempuri.org/SessionInfoHeader">
<User xmlns="">your username goes here</User>
<Password xmlns="">your password goes here.</Password>
</SessionInfo>
</soap:Header>
This is as close as i can get it.
<?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:SessionInfo>
<item>
<key>SessionInfo</key>
<value>
<item><key>User</key><value>[username]</value></item>
<item><key>Password</key><value>[password]</value></item>
</value>
</item>
</ns1:SessionInfo>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<searchCriteria/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I am not even sure this is the problem. Any help would be awesome.
In order to get the child nodes instead of the item key/value pairs you have to pass the parameter as an object.
$client = new SoapClient( 'test.wsdl' );
class SessionInfo {
public $User = 'test#example.com';
public $Password = '12345';
}
$sessionInfo = new SessionInfo();
$soap_headers = new SoapHeader( 'http://tempuri.org/SessionInfoHeader',
'SessionInfo', $sessionInfo );
$client->__setSoapHeaders( $soap_headers );
This will output what the support guy said should work. i think you could also create an array and type cast it to an object but i haven't tried that yet.

Categories