PHP SOAP body - need to set schema version - php

I am using a non-wsdl soap call to a web service and I am able to build the request how I need however I need to set a schema Version within the envelope body.
I am able to build this request with the PHP SoapClient:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://schemas.discovery.co.za/vitality">
<env:Body>
<ns1:UsageEligibilityRequest>
<ns1:RequestingEntityNumber>12345678</ns1:RequestingEntityNumber>
<ns1:Entities>
<ns1:EntityNumber>12345678</ns1:EntityNumber>
<ns1:DateOfBirth>1980-01-01</ns1:DateOfBirth>
<ns1:Gender>M</ns1:Gender>
<ns1:FirstInitial>H</ns1:FirstInitial>
</ns1:Entities>
</ns1:UsageEligibilityRequest>
</env:Body>
</env:Envelope>
However I need this line:
<ns1:UsageEligibilityRequest>
to be
<ns1:UsageEligibilityRequest schemaVersion="1.0">
Not sure how to do this in PHP using the built in functions/options and appears to be hard to Google.
This is built using code like:
$client->__soapCall('UsageEligibilityRequest', array($options));
Thanks.

There's 2 ways you can do this
$requestingUser = 12345678;
$entityNumber = 12345678;
$dateOfBirth = '1980-01-01';
$gender = 'M';
$firstInitial = 'H';
Either use a SoapVar like:
$paramsSOAPVar = new SoapVar('<ns1:UsageEligibilityRequest schemaVersion="1">
<ns1:RequestingEntityNumber>' . $requestingUser. '</ns1:RequestingEntityNumber>
<ns1:Entities>
<ns1:EntityNumber>' . $entityNumber. '</ns1:EntityNumber>
<ns1:DateOfBirth>' .$requestingUser. '</ns1:DateOfBirth>
<ns1:Gender>' .$gender. 'M</ns1:Gender>
<ns1:FirstInitial>' .$firstInitial. '</ns1:FirstInitial>
</ns1:Entities>
</ns1:UsageEligibilityRequest>', XSD_ANYXML);
Set up your array of parameters:
$params = array(
'schemaVersion' => 1,
'RequestingEntityNumber' => $requestingUser,
'Entities' => array(
'EntityNumber' => $entityNumber,
'DateOfBirth' => $dateOfBirth,
'Gender' => $gender,
'FirstInitial' => $firstInitial
)
);

Related

Soap requests with Curl PHP

How can I make Soap request using Curl php by using below soap format and url? I have tried avaliable solutions online and none of them worked out.
$soap_request = '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:alisonwsdl" 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:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Header>
<credentials>
<alisonOrgId>9ReXlYlpOThe24AWisE</alisonOrgId>
<alisonOrgKey>C2owrOtRegikaroXaji</alisonOrgKey>
</credentials>
</soap:Header>
<SOAP-ENV:Body>
<q1:login xmlns:q1="urn:alisonwsdl">
<email xsi:type="xsd:string">email</email>
<firstname xsi:type="xsd:string">fname</firstname>
<lastname xsi:type="xsd:string">lname</lastname>
<city xsi:type="xsd:string">city</city>
<country xsi:type="xsd:string">country</country>
<external_id xsi:type="xsd:string"></external_id>
</q1:login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
Url
https://alison.com/api/service.php?wsdl
Assuming you already have php_soap extension installed, you can access the SOAP API like this:
<?php
$client = new SoapClient('https://alison.com/api/service.php?wsdl', array(
'stream_context' => stream_context_create(array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
))
));
You might want to define the header for authentication as well
$auth = array(
'alisonOrgId' => '9ReXlYlpOThe24AWisE',
'alisonOrgKey' => 'C2owrOtRegikaroXaji'
);
$header = new SoapHeader('https://alison.com/api/service.php?wsdl', 'credentials', $auth, false);
$client->__setSoapHeaders($header);
Then you can get the list of functions available
// Get function list
$functions = $client->__getFunctions ();
echo "<pre>";
var_dump ($functions);
echo "</pre>";
die;
Or call a function right away, like this:
// Run the function
$obj = $client->__soapCall("emailExists", array(
"email" => "test#email.com"
));
echo "<pre>";
var_dump ($obj);
echo "</pre>";
die;
After struggling for a week I was able to find something in this tutorial here on youtube https://www.youtube.com/watch?v=6V_myufS89A and I was able to send requests to the API successifuly, first read my xml format above before continuing with my solution
$options = array('trace'=> true, "exception" => 0);
$client = new \SoapClient('your url to wsdl',$options);
//if you have Authorization parameters in your xml like mine above use SoapVar and SoapHeader as me below
$params = new \stdClass();
$params->alisonOrgId = 'value here';
$params->alisonOrgKey = 'value here';
$authentication_parameters = new \SoapVar($params,SOAP_ENC_OBJECT);
$header = new \SoapHeader('your url to wsdl','credentials',$authentication_parameters);
$client->__setSoapHeaders(array($header));
print_r($client->__soapCall("Function here",'Function parameter here or left it as null if has no parameter'));
//or call your function by
print_r($client->yourFunction($function_parameters));
}
Hope this will help someone out there struggling with soap requests that contains authentication informations

Incorrect SOAP request format for the message body using __soapcall() PHP

I have used __soapcall() to send a soap requsest.
$parameters = array(
'ID' => '0001',
'TimeStamp' => '2016-06-20T17:03:27Z',
'SenderID' => 'B2eww8',
);
$URL = 'https://example.com';
$client = new \SoapClient(null, array(
'location' => $URL,
'uri' => "http://example.com/SOAP/WSDL/abc.xsd",
'trace' => 1
));
$return = $client->__soapCall("TestSoapRequest", $parameters);
var_dump($return);
Then request looks like.
<SOAP-ENV:Body>
<ns1:TestSoapRequest>
<param0 xsi:type="xsd:string">0001</param2>
<param1 xsi:type="xsd:string">2016-06-20T17:03:27Z</param3>
<param2 xsi:type="xsd:string">B2eww8</param4>
</ns1:TestSoapRequest>
</SOAP-ENV:Body>
But desired format is,
<soapenv:Body>
<ns1:TestSoapRequest xmlns:ns1="http://www.caqh.org/SOAP/WSDL/CORERule2.2.0.xsd">
<ID>0001</ID>
<TimeStamp>2014-05-22T17:03:27Z</TimeStamp>
<SenderID>B2eww8</SenderID>
</ns1:TestSoapRequest>
</soapenv:Body>
How can i achive this.
There is no definition for TestSoapRequest in http://www.caqh.org/SOAP/WSDL/CORERule2.2.0.xsd so if you want to prepare request without it then you should use SoapParam class described here http://php.net/manual/en/soapparam.soapparam.php
Similar problem has been described here PHP Soap non-WSDL call: how do you pass parameters?
A few months ago I had the same problem and was using an XML document. I created a generator file which creates an XML document with structure corrected.
$XMLDocument = new DOMDocument('1.0', 'UTF-8');
$Body = $XMLDocument->createElement('soapenv:Body', null);
$Body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns1', "http://www.caqh.org/SOAP/WSDL/CORERule2.2.0.xsd");
$TestSoapRequest = $XMLDocument->createElement('TestSoapRequest', null);
$element = $XMLDocument->createElement('ID', '0001');
$TestSoapRequest->appendChild($grossWeightElement);
$element = $XMLDocument->createElement('TimeStamp', '2014-05-22T17:03:27Z');
$TestSoapRequest->appendChild($grossWeightElement);
$element = $XMLDocument->createElement('SenderID', 'B2eww8');
$TestSoapRequest->appendChild($grossWeightElement);

How to set this soap header on a SOAP CALL PHP?

<SOAP-ENV:Header>
<Seguridad>
<usuario>0000000000</usuario>
<password>9FDBDE265822755C50dHD5D33B61580736ECB94978BC40DDD2D4220CB63FE7E</password>
<fechaSistema>02/01/2015</fechaSistema>
</Seguridad>
</SOAP-ENV:Header>
I need to add this header to the soap call, but, the examples that I found need some namespace, and result so different of this.
I used NuSoap.php
$wsdl = "https://www.convolmiscelaneapruebas.pemex.com/ServiciosCVWEB/ServicioEnviaCONVOLService/ServicioEnviaCONVOLService.wsdl";
$client = new nusoap_client($wsdl,TRUE);
$header =
"<Seguridad>
<usuario>0001250000</usuario>
<password>4e671bf08913dn8k09d6359262117c8e67a5507b165f727288a487040bf2a1780</password>
<fechaSistema>02/01/2015</fechaSistema>
</Seguridad>";
$operation = array('arg0' => '2012-08-25',
'arg1' => '11:48:46',
'arg2'=>'12095866a6l9b7dbcd44640189c57hlZ5918192739040eb52ba5b==',
'arg3'=>'ZmU5866axMzc3ZDQyJmdAyZjM2YTc57Db7dbcd446401YTM1Mg3Ng=='
);
$client->setHeaders($header);
$res = $client->call('enviaCONVOL',$operation);

PHP NuSOAP parameters not included in request

I am trying to send a SOAP request with a header and lots of parameters. This is not the first time I have used NuSOAP and have never had problems with it before. However what is new to me is I am including a header which may be what is causing the problem. Below is the code for my request:
$client = new nusoap_client($url,'wsdl','','','','');
$header =
"<ETGHeader>
<VersionRequest>1.0.0</VersionRequest>
<Originator>
<Signature>Signature</Signature>
<LoginData>
<Name>Name</Name>
<Password>Password</Password>
</LoginData>
</Originator>
</ETGHeader>";
$client->setHeaders($header);
$param = array(
"Settings" => array(
"param1" => "1",
"param2" => "2",
"param3" => "3"
)
);
// Call the WebService
$result = $client->call('GetListVehicleType', array('parameters' => $param), '', '', false, true);
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
Here is the request that is being sent:
<?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/"><SOAP-ENV:Header><ETGHeader>
<VersionRequest>1.0.0</VersionRequest>
<Originator>
<Signature>Signature</Signature>
<LoginData>
<Name>Name</Name>
<Password>Password</Password>
</LoginData>
</Originator>
</ETGHeader></SOAP-ENV:Header><SOAP-ENV:Body><GetListVehicleType xmlns="http://url"/></SOAP-ENV:Body></SOAP-ENV:Envelope>
None of my parameters are being included in the request.
Any help much appreciated
Alex
Might be a problem with NuSOAP, recoded it using PHP's inbuilt SoapClient and got it working.

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