I am trying to connect to a SOAP webservice using php. I am very new to using php.
I can connect to the service, the test below returns a list of all the available functions of the webservice.
$url = "http://...client_ip.../dkServiceDefault/dkWSItemsCGI.exe/wsdl/IItemService";
$client = new SoapClient($url);
var_dump($client->__getFunctions());
If I try to access one of these functions(ex. NumberOfModifiedItems) then I get an error stating that I need to supply a SOAP header with a username and password.
According to the documentation of the SOAP service the header needs to look like this:
<soap:Header>
<q1:BasicSecurity id="h_id1" xmlns:q1="urn:dkWSValueObjects">
<Username xsi:type="xsd:string">username</Username>
<Password xsi:type="xsd:string">password</Password>
</q1:BasicSecurity>
</soap:Header>
How can I make this header in php? How do I attach it to the SoapClient? I have a username and password but I can't figure out how to create the exact header to send to the webservice. I have tried following several tutorials, but I just can't seem to get it to work.
You may pass SOAP headers with SoapHeader class and SoapClient::__setSoapHeaders method:
<?php
$url = "http://...client_ip.../dkServiceDefault/dkWSItemsCGI.exe/wsdl/IItemService";
$client = new SoapClient($url);
$namespace = "urn:dkWSValueObjects";
$authentication = array(
'Username' => 'yourname',
'Password' => 'yourpassword'
);
$header = new SoapHeader($namespace, 'BasicSecurity', $authentication, false);
$client->__setSoapHeaders($header);
var_dump($client->__getFunctions());
?>
Related
$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.
I want to retrieve the value of a parameter from my salesforce instance. For example, I want to recover the trusted IP range:
https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_securitysettings.htm
To do this, use the Metadata API. To access to this API, I use the force.com toolkit for PHP.
However, the examples given only deal with the creation or the update of the parameters:
https://developer.salesforce.com/blogs/developer-relations/2011/11/extending-the-force-com-toolkit-for-php-to-handle-the-metadata-api.html
How to simply get the value of the parameter (for example the trusted IP range)?
The PHP toolkit shipped by Salesforce is quite outdated and should not be used. There are more recent forks/community projects (one,two) that attempt to implement a modern PHP client, perhaps one of these will work for you.
A simple(r) solution is to retrieve the SecuritySettings via a plain SOAP call to the Metadata API. The request payload with API version set to 46.0 should be
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Security</members>
<name>Settings</name>
</types>
<version>46.0</version>
</Package>
and the response looks like this (only relevant portion is shown, the actual response is much larger)
<?xml version="1.0" encoding="UTF-8"?>
<SecuritySettings xmlns="http://soap.sforce.com/2006/04/metadata">
<networkAccess>
<ipRanges>
<description>...</description>
<end>1.255.255.255</end>
<start>0.0.0.0</start>
</ipRanges>
</networkAccess>
</SecuritySettings>
Translating to PHP:
$wsdl = PUBLIC_PATH . '/wsdl-metadata.xml';
$apiVersion = 46.0;
$singlePackage = true;
$members = 'Security';
$name = 'Settings';
$params = new StdClass();
$params->retrieveRequest = new StdClass();
$params->retrieveRequest->apiVersion = $apiVersion;
$params->retrieveRequest->singlePackage = $singlePackage;
$params->retrieveRequest->unpackaged = new StdClass();
$params->retrieveRequest->unpackaged->version = $apiVersion;
$params->retrieveRequest->unpackaged->type = new stdClass();
$params->retrieveRequest->unpackaged->type->members = $members;
$params->retrieveRequest->unpackaged->type->name = $name;
$option = [
'trace' => TRUE,
];
// Namespaces
$namespace = 'http://soap.sforce.com/2006/04/metadata';
$client = new SoapClient($wsdl, $option);
$header = new SoapHeader($namespace, "SessionHeader", array ('sessionId' => $token)); // NEED: access token
$client->__setSoapHeaders($header);
$client->__setLocation($serverUrl); // NEED: service endpoint URL
$serviceResult = $client->retrieve($params);
You'll need to provide an access token ($token) and a service endpoint ($serverUrl).
For anyone trying to get this to work, identigral's example didn't work for me, had to do the following:
change $client->setEndpoint($serverUrl); to $client->__setLocation($serverUrl);
I was using Oauth to login, so you'll need to construct the $serverUrl from the response:
<instance_url> + '/services/Soap/m/46.0/' + <org id (from id)>
Example:
'https://your-production-or-sandbox-name.my.salesforce.com/services/Soap/m/46.0/your-org-id'
I have a client call with a WSSE Security Header:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsse:UsernameToken wsu:Id="UsernameToken-7BCCD9337425FBA038149772606059420"><wsse:Username>USERNAME</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">PASSWORD</wsse:Password><wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">NONCE</wsse:Nonce><wsu:Created>2017-06-17T19:01:00.594Z</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header>
<soapenv:Body>
<ns:FUNCTION/>
</soapenv:Body>
</soapenv:Envelope>
As SoapServer I have a simple one:
// the SOAP Server options
$options=array(
'trace' => true
, 'cache_wsdl' => 0
, 'soap_version' => SOAP_1_1
, 'encoding' => 'UTF-8'
);
$wsdl = 'http://localhost/index.php?wsdl';
$server = new \SoapServer($wsdl, $options);
$server->setClass('ServerClass');
$server->handle();
$response = ob_get_clean();
echo $response;
As soon the property MustUnderstand = 1, then I become from the server the exception: Header not understood.
How to understand the header?
How to make the WSSE validation on the SoapServer side?
The solution is very tricky! I don't know why is this handled by this way from the SoapServer, but here is the solution:
class ServerClass {
public function Security($data) {
// ... do nothing
}
public function myFunction(){
// here the body function implementation
}
}
We need to define a function in our class, which is handling the soap request with the name of the header tag, which is holding the soap:mustUnderstand property. The function doesn't need to be implemented in some way.
That's all!
Mutatos' question / answer got me on the right track. I was working outside of a class structure so what worked for me was the following:
function Security($data)
{
$username = $data->UsernameToken->Username;
$password = $data->UsernameToken->Password;
//check security credentials here
}
$server = new SoapServer("schema/wsdls/FCI_BookingPullService.wsdl", array('soap_version' => SOAP_1_2));
$server->addFunction("Security");
$server->handle();
Essentially a function with the same name as the SOAP header "<wsse:Security>" (ignore the namespace) is being defined, then telling the server to use that to process the header with the 'addFunction' method.
Not ideal from a scope point of view, if that's an issue, try the class approach.
I need to send the following SOAP request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:dat="http://touricoholidays.com/WSDestinations/2008/08/DataContracts">
<soapenv:Header>
<dat:LoginHeader>
<dat:username>myUserName</dat:username>
<dat:password>myPassword</dat:password>
<dat:culture>en_US</dat:culture>
<dat:version>8</dat:version>
</dat:LoginHeader>
</soapenv:Header>
<soapenv:Body>
<dat:GetDestination>
<dat:Destination>
<dat:Continent>Europe</dat:Continent>
<dat:Country>Spain</dat:Country>
<dat:State></dat:State>
<dat:City>Madrid</dat:City>
<dat:Providers>
<dat:ProviderType>Default</dat:ProviderType>
</dat:Providers>
</dat:Destination>
</dat:GetDestination>
</soapenv:Body>
</soapenv:Envelope>
I am trying to achieve this using PHP's built in SoapClient Class. When I run the following code it says "Login failure please check user name and password." But I am very much sure that both the username and password are correct as the same values are being used in other applications.
I think the problem is in the code below. Could you please tell me what is the mistake ?
try{
$client = new SoapClient($soap_url, array("trace" => 1));
$ns = 'http://touricoholidays.com/WSDestinations/2008/08/DataContracts';
$auth = array(
'username' => 'myUserName',
'password' => 'myPassword',
'culture' => 'en_US',
'version' => '8',
);
$header = new SoapHeader($ns, 'LoginHeader', $auth);
$client->__setSoapHeaders($header);
$res = $client->__soapCall("GetDestination", array());
var_dump($res);
}
catch(Exception $e)
{
echo $e->getMessage();
}
you should definitively use a WSDL to php generator that still uses the native SoapClient class such as the PackageGenerator project. It simply generates the PHP SDK according to the WSDL. Then you only have to use the generated classes to construct and send your request. The response is then an object using the generated classes.
I'm trying to communicate with the eWay server and had everything working until we ended up needing to switch to a different API. The problem is that SoapClient is creating a different namespace for the header (that includes the authentication) then from the body, which, obviously, doesn't get me any results. Instead, I get eWay's servers saying it must have the authentication information.
Here's my code:
$client = new SoapClient($url.'?WSDL', array('trace'=>TRUE));
// Set our SOAP Headers for authentication
$header_body = array(
'eWAYCustomerID' => $gateway['customer_id'],
'Username' => $gateway['username'],
'Password' => $gateway['password']
);
$header_var = new SoapVar($header_body, SOAP_ENC_OBJECT);
$header = new SOAPHeader('http://www.eway.com.au/gateway/managedpayment', 'eWAYHeader', $header_body);
//$client->__setSoapHeaders($header);
try {
$response = $client->__soapCall($action, $xml, null, $header);
} catch (SoapFault $e)
{
echo 'SOAP Fault: '. $e->getMessage()."<br>\n";
}
As you can see, I've tried it with and without using a SoapVar for the header, all with no luck.
Here's the XML request that is being created:
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://www.eway.com.au/gateway/managedpayment" xmlns:ns2="http://www.eway.com.au/gateway/managedpayment">
<soap-env:header>
<ns2:ewayheader>
<ewaycustomerid>87654321</ewaycustomerid>
<username>test#eway.com.au</username>
<password>test123</password>
</ns2:ewayheader>
</soap-env:header>
<soap-env:body>
<ns1:createcustomer>...</ns1:createcustomer>
This may be an obvious question, but did you try specifying the typename and type namespace in the SoapVar() call?