SOAP Request using PHP SoapClient - php

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.

Related

SoapClient attributes on called function element

How can I add attributes to the XML node of the soap method I call via SoapClient?
Long story ->
I want to get an authentication token from Zimbra via the SOAP API (https://files.zimbra.com/docs/soap_api/8.6.0/api-reference/zimbraAdmin/Auth.html). I can do it when I build the soap xml by myself
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:zimbra" xmlns:urn1="urn:zimbraAdmin">
<soapenv:Header>
<urn:context />
</soapenv:Header>
<soapenv:Body>
<urn1:AuthRequest name="'. $user .'" password="'. $password .'" />
</soapenv:Body>
</soapenv:Envelope>
But I can't get the name and password attributes as attributes on the AuthRequest (the soap endpoint I am calling) when using SoapClient:
$soap = new SoapClient(ZIMBRA_WSDL, array('trace' => true));
$result = $soap->authRequest(
array(
new SoapParam(ZIMBRA_ADMIN, 'account'),
new SoapParam(ZIMBRA_PASSWORD, 'password')
)
);
It produces always new child elements for AuthRequest whatever I'm trying.
Anybody can help?
I tried lot of recommendations w/o any success.
The problem was that there are two methods authRequest in different namespaces. I've changed the WSDL url to only retrieve the zimbraAdmin endpoint and then SoapClient was able to fill the request correct via:
$result = $soap->authRequest(
array(
'name' => $user,
'password' => $password
)
);

Headers for connecting to a SOAP webservice using PHP

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());
?>

PHP soap request with certifcate

$method ='MerchantFinancialOperationWS';
$configs = array(
'soap_version' => SOAP_1_2,
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => false,
'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
'local_cert' => $cert_file,
'passphrase' => $cert_password
);
if($debug) $configs['trace'] = true;
if(substr($url, -5) != '?WSDL') $url.= '?WSDL';
$webService = new SoapClient($url, $configs);
$data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fin="http://financial.services.merchant.channelmanagermsp.sibs/">
<soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:Action>http://financial.services.merchant.channelmanagermsp.sibs/MerchantFinancialOperationWS/requestFinancialOperationRequest</wsa:Action>
<wsa:ReplyTo>
<wsa:Address>https://enderecodeteste.pt</wsa:Address>
</wsa:ReplyTo>
</soapenv:Header>
<soapenv:Body>
<fin:requestFinancialOperation>
<arg0>
<messageType>N0003</messageType>
<aditionalData>TESTE</aditionalData>
<alias>
<aliasName>351#994999999</aliasName>
<aliasTypeCde>001</aliasTypeCde>
</alias>
<financialOperation>
<amount>400</amount>
<currencyCode>9782</currencyCode>
<operationTypeCode>022</operationTypeCode>
<merchantOprId>11111</merchantOprId>
</financialOperation>
<merchant>
<iPAddress>255.255.255.255</iPAddress>
<posId>880924 </posId>
</merchant>
<messageProperties>
<channel>01</channel>
<apiVersion>1</apiVersion>
<channelTypeCode>VPOS</channelTypeCode>
<networkCode>MULTIB</networkCode>
<serviceType>01</serviceType>
<timestamp>2014-10-31T13:58:49.2934+01:00</timestamp>
</messageProperties>
</arg0>
</fin:requestFinancialOperation>
</soapenv:Body>
</soapenv:Envelope>';
$result = $webService->requestFinancialOperation($data);
I've been trying to implement a soap request with a pem certificate and i'm just getting out of ideas. I know my code should be all wrong but i have no idea what the right direction is. I've been researching but found little no none documentation on this and the team behind the webservice i have to use also wasn't able to help.
I can already communicate with the service using SoapUI so i know the webservice works
Part of the issue is you're sending XML when you may not need to. PHP's built-in SOAP client will handle the XML for you, so you can focus on the objects (the O in SOAP!). You need to construct a data structure (object or array) and pass that to the operation you want to run. First look for the signature of the operation you want using:
$webService = new SoapClient($url, $configs);
var_dump($webService->__getFunctions());
This gives you the API - note that it indicates the data structures it expects in both the input parameters and the output. To see what those data structures look like:
var_dump($webService->__getTypes());
Now you can construct a PHP object with the same fields and structure and pass it in. Your code will look something along these lines:
$webService = new SoapClient($url, $configs);
$parameter = new stdClass();
$parameter->someField = 'N0003';
$parameter->anotherField = 'TESTE';
$result = $webService->requestFinancialOperation($parameter);

PHP get data from soap service

I need to get data from http://ws.jrtwebservices.com/jrtlowfaresearch/jrtlfs.asmx, this service need credential information.such as ID, userid and system value. I put these information into one string:
$xml_post_string = "<POS><Source> <RequestorID Type='21' ID='xxx'/> </Source> <TPA_Extensions> <Provider><System>xxx</System> <Userid>xxx</Userid> </Provider></TPA_Extensions></POS>"
And i also defined SoapClient:
$client = new SoapClient(null, array('uri' => "http://ws.jrtwebservices.com",
'location => "http://ws.jrtwebservices.com/jrtlowfaresearch/jrtlfs.asmx") );
I call soapCall as:
$response = $client->__soapCall('do_LowfareSearch',array($xml_post_string),array('soapaction' => 'http://jrtechnologies.com/do_LowfareSearch'));
Does anybody know why i get empty response?
Thanks very much!
Using your code, the request looks like this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xm...">
<SOAP-ENV:Body>
<ns1:do_LowfareSearch>
<param0 xsi:type="xsd:string">
"<POS><Source> <RequestorID Type='21' ID='xxx'/> </Source> <TPA_Extensions> <Provider <System>xxx</System> <Userid>xxx</Userid> </Provider></TPA_Extensions></POS>"
</param0>
</ns1:do_LowfareSearch>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The client used the method you passed but could not structure the parameters the way you gave them. All of your parameters are just in " " inside of <param0>.
(Also, you are missing a ' after location. 'location => "http:...)
When you make your SOAP client you want to set the WSDL, it will do all the XML formatting for you.
The WSDL should have the location in it so you do not need to worry about that.
I like to use a WSDL validator to test out the methods and see their parameters.
You should structure the information you want to pass as arrays or a classes and let the SOAP client and WSDL convert it into the XML you need.
So something like this is what you are looking for:
<?php
//SOAP Client
$wsdl = "http://ws.jrtwebservices.com/jrtlowfaresearch/jrtlfs.asmx?WSDL";
$client = new SoapClient($wsdl, array( 'soap_version' => SOAP_1_1,
'trace' => true, //to debug
));
try {
$args = array(
'companyname'=> 'xxx',
'name'=> 'xxx',
'system'=> 'xxx',
'userid'=> 'xxx',
'password'=> 'xxx',
'conversationid'=>'xxx',
'entry'=> 'xxx',
);
$result = $client->__soapCall('do_LowfareSearch', $args);
return $result;
} catch (SoapFault $e) {
echo "Error: {$e}";
}
//to debug the xml sent to the service
echo($client->__getLastRequest());
//to view the xml sent back
echo($client->__getLastResponse());
?>

Why is PHP's SoapClient creating a different namespace for the header than the body?

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?

Categories