How to make PHP SOAP Header like this xml snippet? - php

I'm trying to make a soap header that would look like this:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<a-user xmlns="a">testName</a-user>
<a-pass xmlns="a">testPassword</a-pass>
</s:Header>
<s:Body>
...
</s:Body>
</s:Envelope>
I'm pretty new to soap so I'm pretty sure I just don't understand what I'm supposed to put in for the parameters of the SoapHeader constructor, this is what I'm currently trying and it keeps failing to authenticate:
$authHeader = new SoapHeader("http://schemas.xmlsoap.org/soap/envelope/", "Envelope", array("Header" => array("a-user" => "testName", "a-pass" => "testPassword")));
Can someone explain how I'm supposed to translate that header from the xml into a php soapheader please?
Edit: Yes, agunn, I'm using a real user name and pass, I get a SoapFault exceptions saying "Vendor not authorized!" when I make my soapCall.
Here's the soap call part included:
$this->client->__setSoapHeaders($authHeader);
$response = $this->client->__soapCall("Foo", $params);

$authHeader = new SoapHeader("http://schemas.xmlsoap.org/soap/envelope/", "Envelope", array("Header" => array("a-user" => "testName", "a-pass" => "testPassword")));
By the SoapClient spec you should try like this
$this->client->__setSoapHeaders(array($authHeader));
$response = $this->client->__soapCall("Foo", $params);

Related

How to set wsa true and ws addressing true in pure php of soap webservice

$data2=[
"user_name"=>"****",
"password"=>"****",
"customer_num"=>"***",
"process_num"=>000,
];
$wsdl = "https://vbtestservice.vakifbank.com.tr/HesapHareketleri.OnlineEkstre/SOnlineEkstreServis.svc?singleWsdl";
$client =new SoapClient($wsdl,array("trace"=>true,"exceptions"=>0));
$a=$client->__soapCall('GetirDekont', $data2);
echo $client->__getLastResponse();
Result of php : looks like we got no xml document
or response
The requested URL was rejected. Please consult with your administrator."
But
As shown in the pictures the wdsl is working at he SoapUI but how to implement the options in the pure php or in the laravel framework. Thank you you for helping !!
SoapUI return response this xml:
xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">
Peak.Integration.ExternalInbound.Ekstre/ISOnlineEkstreServis/GetirDekontResponse
</a:Action>
</s:Header>
<s:Body>
<GetirDekontResponse xmlns="Peak.Integration.ExternalInbound.Ekstre">
<GetirDekontResult
xmlns:b="http://schemas.datacontract.org/2004/07/Peak.Integration.ExternalInbound.Ekstre.DataTransferObjects"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<b:IslemKodu>VBD0004</b:IslemKodu>
<b:IslemAciklamasi>Talimat için dekont bulunamadı.</b:IslemAciklamasi>
<b:DekontListesi xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
</GetirDekontResult>
</GetirDekontResponse>
</s:Body>
</s:Envelope> ```
I found the solution :)
Quote from Alper Yaman, a member of Laravel Turkey group on Facebook.
To set WSA Addressing and add default wsa:To options on SoapClient connection in Laravel, you can follow these steps:
You can specify "soap_version" and "features" in the options array when creating the SoapClient object. Example:
$options = array(
'soap_version' => SOAP_1_2,
'features' => SOAP_SINGLE_ELEMENT_ARRAYS
);
$client = new SoapClient($wsdl, $options);
You can set WSA Addressing and add default wsa:To options by editing the headers of the request sent to the SoapClient object. Example:
$headers = array(
new SoapHeader("http://www.w3.org/2005/08/addressing", "Peak.Integration.ExternalInbound.Ekstre/ISOnlineEkstreServis/GetirDekont", $to),
new SoapHeader("http://www.w3.org/2005/08/addressing", "WEB SERVICE LINK ENDING WITH ?WSDL HERE", $action)
);
$client->__setSoapHeaders($headers);
Note: The $to and $action variables are required values for WSA Addressing.
After doing these operations, try to connect to the web service with the SoapClient object.
If you still get the error, please contact the server's administrator to ensure that the URL is not rejected.

SOAP request in PHP in CURL

It's have been already several days since I'm trying to figure out SOAP but no success :( Any chance to know how can I create PHP(curl) SOAP request to look like this?
POST /WebServices/domain.asmx HTTP/1.1
Host: webservices.domain.ru
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.domain.ru/GetVariants"
<?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:Header>
<AuthentificationHeader xmlns="http://www.domain.ru/">
<Login>string</Login>
<Password>string</Password>
<PartnerId>string</PartnerId>
</AuthentificationHeader>
</soap:Header>
<soap:Body>
<GetVariants xmlns="http://www.domain.ru/">
<RequestParameters>xml</RequestParameters>
</GetVariants>
</soap:Body>
</soap:Envelope>
Also I have following data:
WSDL:
http://webservices.domain.ru/WebServices/domainXml.asmx?WSDL
Soap action:
http://webservices.domain.ru/WebServices/domainXml.asmx?op=GetVariants
Basically:
PHP's support for SOAP just sucks.
You need to use your WSDL to generate your PHP classes, so you can build a Request object.
You will get something like this:
class SomeClass
{
var $name; //NCName
}
class SomeOtherClass
{
var $value; //anonymous2
}
.
.
.
There are some WSDL to PHP scripts out there. (Google wsdl to php)
Then you do something like this.
$soapClient = new SoapClient($this->_wsdlUrl, array(
'trace' => true,
'exceptions' => true,
'uri' => $this->_endPoint,
'location' => $this->_endPoint,
'local_cert' => $this->certificatePath,
'allow_self_signed' => true,
'soap_version' => SOAP_1_2,
));
// Build the SOAP client object, with your properties.
//After that, you have to call:
$yourContainerObject = new YourContainerObject(); // The top SOAP XML node
$yourContainerObject->SomeChildNode = new SomeChildNode();
$yourContainerObject->SomeChildNode->Name = 'John';
.
.
.
$soapResponse = $soapClient->GetVariants($yourContainerObject);
print_r(soapResponse); // to see what you get
The "GetVariants" method doesn't need to be implemented, so you don't get confused and ask yourself where do I get that from.It is described in WSLD and PHP's SOAP client knows that it is just a SOAP action.
This should be an more advanced example of creating SOAP requests via PHP.
I hope you get the basic flow of this.

Converting xml response to Array with php

Please i'm developping a php application, i want to know how to convert the xml reponse of a soap webservice into strings or array or json, i want to insert the values returned by the webservice into my database.
here is my xml respnse:
<env:Envelope xmlns:env="">
<env:Header/>
<env:Body>
<soap:getUserInfoResponse xmlns:soap="">
<getUserInfoResult>
<userInformation>
<detailuserInformationList>
<detailuserInformation>
<Name>Alex</Name>
<alias>Lex</alias>
<age>24</age>
<function>agent</function>
<birthdate>1</birthdate>
<birthmounth>02</birthmounth>
<birthyear>1990</birthyear>
<address>wall street</address>
<civility>mr</civility>
<gender>m</gender>
</detailuserInformation>
</detailuserInformationList>
<lastLogin>14:22</lastLogin>
<userId>A52241</userId>
<cnxTimes>125</cnxTimes>
</userInformation>
<requestTag/>
</getUserInfoResult>
</soap:getUserInfoResponse>
</env:Body>
</env:Envelope>
and here is my php code, calling the ws:
$client = new SoapClient('$url', array("trace" => 1, "exception" => 0));
$client->__setLocation('$url');
$response = $client->__soapCall("getUserInfo", array( parameters...);
print htmlspecialchars($client->__getLastResponse());
Try to use php SoapClient class
SoapClient class
If you generate WSDL use
NuSOAP
Firstly, check what do you have in $response in your code:
print htmlspecialchars($response);
Sceondly, this is proper way or working with SoapClient:
$client = new SoapClient($wsdl_url, array('trace' => 1, 'exception' => 0));
$response = $client->getUserInfo( /* parameters */ );
You'll get array in $response. You can laso view request and response XML with $client->__getLastRequest() and $client->__getLastResponse().

PHP SOAP Errormessage - System.NullReferenceException: Object reference not set to an instance of an object

I have this problem, currently I am learning soap and developing online booking system with wsdl of http://vanillatours.com in PHP
their required headers are soapaction, and charset. which I have included, soapaction changes for desired request. for example, currently trying to do checklogin() function.
I tried print_r($client->__getFunctions()) to see if functions are attached, they are!
I tried print_r($client) to see if headers are attached and they are
the problem is that I can not figure out why I get this errormessage.
System.NullReferenceException: Object reference not set to an instance of an object.
at WcfService.Wcf.CheckLogin(LoginHeaderWcfRequest loginHeader)
Tried everything! I am very new with soap and any help would be appreciated. maybe I am not using correctly data "request"?
thank you!
<?php
$wsdl = "http://xmltest.vanillatours.com/Wcf.svc?wsdl";
$client = new SoapClient($wsdl);
$data = array(
"request" => array(
"a:AgentId" => blabla,
"a:Language" => "En",
"a:Password" => "blabla",
"a:Username" => "blabla"
)
);
$header = array();
$header[] = new SoapHeader('http://tempuri.org/IWcf/CheckLogin','SOAPAction');
$header[] = new SoapHeader('text/xml; charset=utf-8','ContentType');
$client->__setSoapHeaders($header);
$response = $client->__soapCall('CheckLogin', $data);
echo '<pre>';
print_r($client->__getFunctions()); // functions seem to show pretty well
echo '<br>------------------------------------------------------<br><br>';
print_r($client); // headers are attached
echo '<br>------------------------------------------------------<br><br>';
print_r($response); // errormessage, can not figure out what is the problem.
echo '</pre>';
?>
this is how to connect from their documentation, if I can use other method, would appreciate too.
CheckLogin function checks the user credentials is valid or not.
SOAPAction value is http://tempuri.org/IWcf/CheckLogin
3.1.2 Request
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<CheckLogin xmlns="http://tempuri.org/">
<loginHeader xmlns:a="http://schemas.datacontract.org/2004/07/WcfService"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:AgentId>Your Agent Id</a:AgentId>
<a:Language>Your preferred language</a:Language>
<a:Password>Your Password</a:Password>
<a:Username>Your username</a:Username>
</loginHeader>
</CheckLogin>
</s:Body>
</s:Envelope>
The structure of $data is not right. It should be:
$data = array(
"loginHeader" => array(
"AgentId" => blabla,
"Language" => "En",
"Password" => "blabla",
"Username" => "blabla"
)
);

PHP SoapClient: Action mismatch

I am trying to use the PHP SoapClient extension to communicate with an external SOAP server.
This is my code:
$this->_client = new SoapClient(SOAP_TAGGING_URL, array(
'trace' => 1,
'soap_version' => SOAP_1_2,
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => true,
'uri' => SOAP_URI,
));
try {
$actionHdr = array();
$actionHdr[] = new SoapHeader(SOAP_TAGGING_URL, 'Action', 'GetMessagesByTagsByGroup');
$this->_client->__setSoapHeaders($actionHdr);
$info = $this->_client->GetMessagesByTagsByGroup(
new SoapParam($this->params['mPID'], 'ParentMessageID'),
new SoapParam($gid, 'GroupId'),
new SoapParam(REQUEST_TOKEN, 'RequestToken'),
new SoapParam(ACCESS_TOKEN, 'AccessToken'),
new SoapParam(ACCESS_TOKEN_SECRET, 'AccessTokenSecret')
);
} catch (SoapFault $fault) {
print("\n<br/>SOAP server returned the following ERROR: ".$fault->faultcode."-".$fault->faultstring);
}
echo "\n<br/>SOAP request: ". htmlentities($this->_client->__getLastRequest());
echo "\n<br/>SOAP response: ". htmlentities($this->_client->__getLastResponse());
This is the response I get (formatting added):
SOAP server returned the following ERROR: s:Sender-The SOAP action specified on the message, '', does not match the HTTP SOAP Action, 'http://tempuri.org/ITagging/GetMessagesByTagsByGroup'.
SOAP request:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://tempuri.org/" xmlns:ns2="https://mywebserver.com/myWSDL.svc/ws?wsdl">
<env:Header>
<ns2:Action>GetMessagesByTagsByGroup</ns2:Action>
</env:Header>
<env:Body>
<ns1:GetMessagesByTagsByGroup/>
<GroupId>2178</GroupId>
<RequestToken>odwedwo09i0jACqbbjsw6KnlCA=</RequestToken>
<AccessToken>OlVbHurPJrNrEFR54Y0hV9kI/TZs=</AccessToken>
<AccessTokenSecret>js1kerfe453FLuaXpL 892DY o=</AccessTokenSecret>
</env:Body>
</env:Envelope>
SOAP response:
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.w3.org/2005/08/addressing/fault</a:Action>
</s:Header>
<s:Body>
<s:Fault>
<s:Code>
<s:Value>s:Sender</s:Value>
<s:Subcode>
<s:Value>a:ActionMismatch</s:Value>
</s:Subcode>
</s:Code>
<s:Reason>
<s:Text xml:lang="en-US">The SOAP action specified on the message, '', does not match the HTTP SOAP Action, 'http://tempuri.org/ITagging/GetMessagesByTagsByGroup'. </s:Text>
</s:Reason>
<s:Detail>
<a:ProblemHeaderQName>a:Action</a:ProblemHeaderQName>
</s:Detail>
</s:Fault>
</s:Body>
</s:Envelope>
I thought I added the 'Action' parameter in the header, but clearly that is not the place to put it. Or am I doing something else wrong?
Unfortunately I cannot try NuSoap because I have no control over the server.
Thank you,
gm
It means meant that not only you must specify the HTTP header SOAPAction: "http://www.bla.com:MyAction"
but you need to specify also in the SOAP Envelope the header:
Check these links for some ref:
SOAP ACTION
You're passing it as SoapHeader, but actually it is HTTP header.
The way I found to do it is: http://lt.php.net/manual/en/soapclient.dorequest.php by setting $action parameter.
You'll probably need to extend SoapClient class to do it using less lines of code.

Categories