How to set SOAP request to use SoapClient in php? - php

I am trying to use php SoapClient to consume this SOAP api but which seems to be not working. Can any of you please guide me?
This is the request sample:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:par="http://www.example.com/interface/">
<soapenv:Header>
<par:Header>
<par:UserName>USERNAME</par:UserName>
<par:Password>PASSWORD</par:Password>
</par:Header>
</soapenv:Header>
<soapenv:Body>
<par:GetExampResult>
<par:rollNumber>1703011</par:rollNumber>
</par:GetExampResult>
</soapenv:Body>
</soapenv:Envelope>
This is how i tried to convert it to Soap client and not working.
$username = 'USERNAME';
$password = 'PASS';
$soapURL = "http://www.example.com/interface.asmx?WSDL";
$client = new \SoapClient($soapURL);
$auth = [
'SamplingSoapHeader' => [
'UserName' => $username,
'Password' => $password,
],
];
$header = new \SoapHeader('http://www.example.com/interface/',
'RequestorCredentials', $auth);
$client->__setSoapHeaders($header);
$response = $client->GetExampResult(["rollNumber" => "1703011"]);
print_r($response);
Thanks

You wrapped your Soapheader into an additional XML Tag:
<RequestorCredentials>
<SamplingSoapHeader>
<Username>XYZ</Username>
<Password>123</Password>
</SamplingSoapHeader>
<RequestorCredentials>
In the Soapheader ctor Set the Name to SamplingSoapHeader instead of RequestorCredentials and let $auth be an Array containing only Username and Password instead of wrapping it into SamplingSoapHeader.

Related

Interacting with a soap API without WSDL in PHP

Recently got access to a soap API for a hotel management service. They have provided documentation which shows a basic example of a request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<Auth xmlns="http://xxxx/xxxxAPI">
<FromSystemId ID="1">CompanyName</FromSystemId>
<UserName>username</UserName>
<Password>password</Password>
</Auth>
</soapenv:Header>
<soapenv:Body>
<GetRegions Timestamp="2016-04-11" Version="1.0" Lang="en"
xmlns="http://xxxx/xxxxAPI">
<Country Code="GB" />
</GetRegions>
</soapenv:Body>
</soapenv:Envelope>
They have also provided a list of functions in their documentation and the parameters required for each of the functions. But i am abit confused on how to perform a request as i have never used a soap API before. They also haven't provided a WSDL, does this matter?
Anyway, here is how i thought to try and perform a request
$soapURL = "http://xxxx/xxxxAPI" ;
$soapParameters = Array('login' => "username", 'password' => "password") ;
$soapFunction = "getRegions";
$soapFunctionParameters = Array('countrycode' => 'GB');
$soapClient = new SoapClient($soapURL, $soapParameters);
$soapResult = $soapClient->__soapCall($soapFunction,
$soapFunctionParameters) ;
if(is_array($soapResult) && isset($soapResult['someFunctionResult'])) {
// Process result.
} else {
// Unexpected result
if(function_exists("debug_message")) {
debug_message("Unexpected soapResult for {$soapFunction}: ".print_r($soapResult, TRUE)) ;
}
}
Am i going about this the right way? i am unable to test this right now as i haven't received my authentication but wanted to make a start on it now.
Any help would be great.
Here is a small example.
$opts = array(
'location' => 'http://xxxx/xxxxAPI',
'uri' => 'urn:http://test-uri/'
);
$client = new SOAPClient(null, $opts);
$headerData = array(
'FromSystemId' => 'CompanyName',
'UserName' => 'username',
'Password' => 'password',
);
// Create Soap Header.
$header = new SOAPHeader('http://xxxx/xxxxAPI', 'Auth', $headerData);
// Set the Headers of Soap Client.
$client->__setSoapHeaders($header);
$result = $client->__soapCall('getRegions', array('GB'));
// $return = $client->__soapCall('getRegions', array(new SoapParam(new SoapVar('GB', XSD_STRING), 'countryCode')));
var_dump($result);
They also haven't provided a WSDL, does this matter?
To be able to add the HEADER attributes they must be mentioned in WSDL. If they not exist in WSDL they WILL NOT appear as attributes but rather <item><key/><value/></item> elements.
Tipp: If you know how the request has to be and you have no WSDL, then try to generate the HTTP header and XML body manually and execute the request with CURL or Guzzle.
Example with Guzzle:
$soapContent = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<Auth xmlns="http://xxxx/xxxxAPI">
<FromSystemId ID="1">CompanyName</FromSystemId>
<UserName>username</UserName>
<Password>password</Password>
</Auth>
</soapenv:Header>
<soapenv:Body>
<GetRegions Timestamp="2016-04-11" Version="1.0" Lang="en"
xmlns="http://xxxx/xxxxAPI">
<Country Code="GB" />
</GetRegions>
</soapenv:Body>
</soapenv:Envelope>';
$client = new GuzzleHttp\Client([
'headers' => [ 'SOAPAction' => '"urn:http://xxxx/xxxxAPI/#getRegions"' ]
]);
$response = $client->post('http://xxxx/xxxxAPI',
['body' => $soapContent]
);
echo $response;

PHP SOAP complex types

I have a working XML request body, which I'm trying to send via PHP to a SOAP server:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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:tns="urn:servicewsdl" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<mns:getQuote xmlns:mns="urn:getquoteswsdl" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<authorisation xsi:type="tns:Authorisation">
<username xsi:type="xsd:string">****</username>
<password xsi:type="xsd:string">****</password>
</authorisation>
<symbol xsi:type="xsd:string">****</symbol>
</mns:getQuote>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I tried passing the params in an array like so:
$result = $client->getQuote(array('username' => '****', 'password' => '****'), array('symbol' => '****'));
but it returns me an error:
SOAP-ERROR: Encoding: object has no 'username' property
you probably need to put the username/password array inside authorisation like:
$result = $client->getQuote(array('authorisation'=>array('username' => '****', 'password' => '****')), array('symbol' => '****'));
or, maybe you should send it an object instead:
$params = new stdClass();
$params->authorisation = new stdClass();
$params->authorisation->username = '****';
$params->authorisation->password = '****';
$params->symbol = '****';
$result = $client->getQuote($params);
EDIT: From the comments, in the end, getQuote was accessed via PHP in this method:
$auth = new stdClass();
$auth->username = '****';
$auth->password = '****';
$symbol = '****';
$result = $client->getQuote($auth, $symbol);
I had a heck of a time working with PHP and SOAP as well. I seem to recall a similar situation where I used the standard PHP class (new stdClass()) to help build the object with params. Here's the questions/answer page.
How can I add Attributes to a PHP SoapVar Object?
I hope it helps.

Tiger API Soap request. Highly confusing

I'm at the beginning of setting up the Tiger Air API. They have given few URLs for testing but these doesn't seem to work. Following are the details I received from them
Contact mail:
For testing, we have linked this account to your
xxxxxx agency in TEST (currency SGD).
Username: xxx
P/W: xxx (password will have to be changed on first login)
Domain: xxx
RoleCode: xxx
The URL’s for the API to test for connectivity are as follows:
Test: https://trtestr3xapi.navitaire.com/sessionmanager.svc
For the access to the various services, you will want to make your calls to the following DNS:
Test: https://trtestr3xapi.navitaire.com
SOAP sample request
The above two URLs don't seem to work at all. Its surprising that they did not provide the WSDL url. I have got the sample request from them.
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://schemas.navitaire.com/WebServices"
xmlns:ses="http://schemas.navitaire.com/WebServices/ServiceContracts/SessionService"
xmlns:ses1="http://schemas.navitaire.com/WebServices/DataContracts/Session"
>
<soapenv:Header>
<web:ContractVersion>320</web:ContractVersion>
</soapenv:Header>
<soapenv:Body>
<ses:LogonRequest>
<!--Optional:-->
<ses:logonRequestData>
<!--Optional:-->
<ses1:DomainCode>EXT</ses1:DomainCode>
<!--Optional:-->
<ses1:AgentName>MyAgent</ses1:AgentName>
<!--Optional:-->
<ses1:Password>P#ssword</ses1:Password>
<!--Optional:-->
<ses1:LocationCode></ses1:LocationCode>
<!--Optional:-->
<ses1:RoleCode></ses1:RoleCode>
<!--Optional:-->
<ses1:TerminalInfo></ses1:TerminalInfo>
</ses:logonRequestData>
</ses:LogonRequest>
</soapenv:Body>
</soapenv:Envelope>
I was struggling for a long time trying to figure out the basic setup but going into a loop.
your server ip need to be allowed for requests on this link
<?
$Username = 'xxx';
$Password: 'xxx';
$Domain: 'xxx';
$url = "https://trtestr3xapi.navitaire.com/SessionManager.svc?wsdl";
$client = new SoapClient($url);
$params = array(
'logonRequestData' => array(
'DomainCode' => $Domain,
'AgentName' => $Username,
'Password' => $Password,
)
);
print_r($client->Logon($params));
?>
another useful code
<?
$url = "https://trtestr3xapi.navitaire.com/SessionManager.svc?wsdl";
$client = new SoapClient($url);
print_r($client->__getFunctions());
print_r($client->__getTypes());
?>
and
<?
$url = "https://trtestr3xapi.navitaire.com/BookingManager.svc?wsdl";
$client = new SoapClient($url);
print_r($client->__getFunctions());
print_r($client->__getTypes());
?>

Create this SOAP XML request with PHP's SOAP extension

I'm trying to recreate this XML request using PHP's SOAP extension and having trouble doing it:
<?xml version="1.0"?>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.web.stormpost.skylist.com">
<soapenv:Header>
<authInfo xmlns:soap="http://skylist.com/services/SoapRequestProcessor" xsi:type="soap:authentication">
<!--You may enter the following 2 items in any order-->
<username xsi:type="xsd:string">****#example.com</username>
<password xsi:type="xsd:string">*******</password>
</authInfo>
</soapenv:Header>
<soapenv:Body>
<ser:doImportFromTemplate soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<impTempId xsi:type="xsd:int">7</impTempId>
<data xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soapenc:string">
Joe|Doe|joe#example.com John|Doe|john#example.com </data>
</ser:doImportFromTemplate>
</soapenv:Body>
</soapenv:Envelope>
Here is my testing code so far:
// set connection params
$wsdl = 'http://api.stormpost.datranmedia.com/services/SoapRequestProcessor?wsdl';
$namespace = 'https://api.stormpost.datranmedia.com/services/SoapRequestProcessor';
$credentials = (object)array(
'username' => '****#example.com',
'password' => '*******'
);
$auth_values = new SoapVar($credentials, SOAP_ENC_OBJECT);
$header = new SoapHeader($namespace, "authInfo", $auth_values);
// set client, headers, and call function
$client = new SoapClient($wsdl, array('trace' => true));
$client->__setSoapHeaders(array($header));
$client->__soapCall("doImportFromTemplate", array(7, 'Joe|Doe|joe#example.com John|Doe|john#example.com'));
It doesn't seem to be implementing the authInfo node correctly with the xsi:type="soap:authentication" among other things. How should I change my code to output this XML? I'm having trouble finding good implementation examples.
I struggled with the php SOAP client myself a while ago, i didn't really got a full understanding but this worked for me:
$auth_values = array('username' => '****#example.com', 'password' => '*******');
$header = new SoapHeader($namespace, "authInfo", $auth_values, false);
// set client, headers, and call function
$client = new SoapClient($wsdl, array('trace' => true));
$client->__setSoapHeaders(array($header));

PHP SOAP Header construction

So basically I want the SOAP header to be like this:
<soapenv:Header>
<v1:loginDetails>
<v11:Id>0</v11:Id>
<v11:username>MEMBERS</v11:username>
$ <v11:password>0x909711E5,0xE301F82A,0x0E2783CC,0xAF6BC3DB,0x57727CFB</v11:password>
</v1:loginDetails>
</soapenv:Header>
<soapenv:Body>
<v11:GetNextAvailableMemberNumberRequest>
<v11:Id>1</v11:Id>
<v11:memberId>1</v11:memberId>
</v11:GetNextAvailableNumberRequest>
</soapenv:Body>
</soapenv:Envelope>
But instead, now I have this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www..com/membership/types/v1_0">
<SOAP-ENV:Header>
<ns1:loginDetails>
<item><key>siteId</key><value>0</value></item>
<item><key>Username</key><value>MEMBERSHIP</value></item>
<item><key>Password</key><value>P#ssw0rd</value></item>
</ns1:loginDetails>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetNextAvailableMemberNumberRequest/>
<param1>1</param1>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And this is the php code that I'm currently using:
$client = new SOAPClient('http://192.168.180.128:8010//membershipService?wsdl',array('trace' => true));
$client->__setSoapHeaders(null);
$headerbody = array ('siteId' => '0','Username' => 'MEMBERSHIP',
'Password' => 'P#ssw0rd');
$header = new SOAPHeader('http://www.com/club/services/membership/types/v1_0','loginDetails',$headerbody);
$client->__setSoapHeaders($header);
Where have I gone wrong? I seems unable to construct the header properly.
Just try to use an anonymous class instead of an array :
// Use standard class and set magic properties.
$header = new stdClass();
$header->siteId = '0';
$header->Username = 'MEMBERSHIP';
$header->Password = 'P#ssw0rd';
Casting the array of parameters to an object works pretty well
$auth = (object)array(
'Username' => 'AzureDiamond',
'Password' => 'hunter2',
);
$header = new SOAPHeader('http://my.ns/','loginDetails',$auth);
However, I find that it still doesn't get namespaces quite right. The above produces
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="http://my.ns/">
<SOAP-ENV:Header>
<ns2:loginDetails>
<Username>AzureDiamond</Username>
<Password>hunter2</Password>
</ns2:loginDetails>
</SOAP-ENV:Header>
....
And you'll see that although the 'loginDetails' tag is in the requested namespace, it's child elements are not.
I just encountered a very similar problem.
To fix it, you need to use the SoapVar class. This allows you to control what namespace to use for each tag.
$client = new SOAPClient('http://192.168.180.128:8010//membershipService?wsdl',array('trace' => true));
$ns = 'http://www.com/club/services/membership/types/v1_0';
$headerbody = new SoapVar([
new SoapVar('0', XSD_STRING, null, null, 'siteId', $ns),
new SoapVar('MEMBERSHIP', XSD_STRING, null, null, 'Username', $ns),
new SoapVar('P#ssw0rd', XSD_STRING, null, null, 'Password', $ns),
], SOAP_ENC_OBJECT);
$header = new SOAPHeader($ns,'loginDetails',$headerbody);
$client->__setSoapHeaders($header);
Your request's soap header should look something like this with this:
<SOAP-ENV:Header>
<ns1:loginDetails>
<ns1:siteId>0</ns2:siteId>
<ns1:Username>MEMBERSHIP</ns2:Username>
<ns1:Password>P#ssw0rd</ns2:Password>
</ns1:loginDetails>
</SOAP-ENV:Header>
There was typo:
$header = NEW stdClass() ;
$headerbody = array ('Id' => '0','username' => 'MEMBERSHIP',
'password' => 'P#ssw0rd');
Please refer the link
http://php.net/manual/en/class.soapheader.php
Also make sure that you are passing correct namespace (first parameter) in SOAPHeader constructor

Categories