PHP SoapClient target namespace not in request params - php

I am using PHP SoapClient in WSDL mode.
This is what the expected SOAP request should look like:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns="http://xml.m4u.com.au/2009">
<soapenv:Header/>
<soapenv:Body>
<ns:sendMessages>
<ns:authentication>
<ns:userId>Username</ns:userId>
<ns:password>Password</ns:password>
</ns:authentication>
<ns:requestBody>
<ns:messages>
<ns:message>
<ns:recipients>
<ns:recipient>61400000001</ns:recipient>
</ns:recipients>
<ns:content>Message Content</ns:content>
</ns:message>
</ns:messages>
</ns:requestBody>
</ns:sendMessages>
</soapenv:Body>
</soapenv:Envelope>
And this is what PHP SoapClient is sending:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://xml.m4u.com.au/2009">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns1:sendMessages>
<ns1:authentication>
<userId>Username</userId>
<password>Password</password>
</ns1:authentication>
<ns1:requestBody>
<messages>
<message>
<recipients>
<recipient>61400000001</recipient>
</recipients>
<content>Message Content</content>
</message>
</messages>
</ns1:requestBody>
</ns1:sendMessages>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
This is how I constructed the client and params:
function sendMessages($recipient, $content) {
$authenticationType = new SoapVar(array('userId' => $this->username, 'password' => $this->password), SOAP_ENC_OBJECT);
$recipientsType = new SoapVar(array('recipient' => $recipient), SOAP_ENC_OBJECT);
$messageType = new SoapVar(array('recipients' => $recipientsType, 'content' => $content), SOAP_ENC_OBJECT);
$messagesType = new SoapVar(array('message' => $messageType), SOAP_ENC_OBJECT);
$requestBodyType = new SoapVar(array('messages' => $messagesType), SOAP_ENC_OBJECT);
$params = array(
'authentication' => $authenticationType,
'requestBody' => $requestBodyType
);
try {
$this->soapClient = new SoapClient($this->wsdl, array('trace' => 1));
$this->soapClient->__setSoapHeaders(array());
return $this->soapClient->sendMessages($params);
} catch (SoapFault $fault) {
echo '<h2>Request</h2><pre>' . htmlspecialchars($this->soapClient->__getLastRequest(), ENT_QUOTES) . '</pre>';
trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
}
}
Why is 'ns1' present for 'authentication' and 'requestBody' but missing for their child nodes?
What am I doing wrong? The WSDL is located here => http://soap.m4u.com.au/?wsdl
Appreciate anyone who can help.

You must specify the URI of the namespace to encode the object. This will include everything necessary (including your missing namespace). The acronym of the namespace name is irrelevant.
This:
$authenticationType = new SoapVar(array('userId' => $this->username, 'password' => $this->password), SOAP_ENC_OBJECT);
should be:
$authenticationType = new SoapVar(array('userId' => $this->username, 'password' => $this->password), SOAP_ENC_OBJECT, "authentication","http://xml.m4u.com.au/2009");
The problem you're seeing with the namespace serialization comes from using soapvar without all the parameters. When it serializes the request prior to sending it assumes the namespace is already included. If, instead, you had created each as a simple array and included them in $params it would include the namespace for the internal parameters correctly. By way of demonstration:
$authenticationType = array('userId' => $this->username, 'password' => $this->password)

try using nusoap library. Below is sample code:
<?php
$wsdl = "http://soap.m4u.com.au/?wsdl";
// generate request veriables
$data = array();
$action = ""; // ws action
$param = ""; //parameters
$options = array(
'location' => 'http://soap.m4u.com.au',
'uri' => ''
);
// eof generate request veriables
//$client = new soap_client($wsdl, $options);// create soap client
$client = new nusoap_client($wsdl, 'wsdl');
$client->setCredentials($api_username, $api_password);// set crendencials
$opt = $client->call($action, $param, '', '', false, true);
print_r($opt);
?>

Related

SOAP working with xml via soapui but not with PHP

I am creating an SOAP client using PHP and having issues with consuming. When I test the request direct XML using soapui it responds fine and works but with PHP using SoapClient class it tells me the same credentials which I use in soapui are incorrect.
Not sure what I am missing here. My code below
Below is my XML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pric="http://upshot.co.uk/pricing_request_ws">
<soapenv:Header/>
<soapenv:Body>
<pric:retrieveProductsPricing>
<username>apiuser</username>
<password>pword</password>
<postcode>EC2R 7HP</postcode>
</pric:retrieveProductsPricing>
</soapenv:Body>
</soapenv:Envelope>
Below is my PHP
$wsdl = "http://URL?wsdl";
$client = new SoapClient($wsdl, array('trace'=>1));
try
{
$options = array(
'soap_version'=>SOAP_1_2,
'exceptions'=>true,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE
);
$params = array(
'User name' => 'apiuser',
'Password' => 'pword',
'Postcode' => 'EC2R 7HP'
);
$response = $client->retrieveProductsPricing($params);
print_r($response);
}
catch(SoapFault $e)
{
print_r($e);
This is my first time configuring a soap client so I'm sure I have potentially made a mistake in this.
Have a look at the first code:
<username>apiuser</username>
<password>pword</password>
<postcode>EC2R 7HP</postcode>
You should use the same keys for the array
$params = array(
'username' => 'apiuser',
'password' => 'pword',
'postcode' => 'EC2R 7HP'
);
Useful examples

PHP SOAP client not creating body

After over a half a day of trying and reading tutorials on creating a simple SOAP client, I am no closer to retrieving a request from API I attempting to work with.
WSDL: http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL
I can make the request from SOAP UI with the following simple SOAP request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pub="http://publicapi.ekmpowershop.com/">
<soapenv:Header/>
<soapenv:Body>
<pub:GetOrders>
<!--Optional:-->
<pub:GetOrdersRequest>
<!--Optional:-->
<pub:APIKey>myApiKey</pub:APIKey>
</pub:GetOrdersRequest>
</pub:GetOrders>
</soapenv:Body>
</soapenv:Envelope>
This above returns the expected data.
When it comes to translating the request into a PHP I have the following:
$wsdl = 'http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$param = array('GetOrdersRequest' => array(
'APIKey' => 'myApiKey'
)
);
$resp = $client->GetOrders();
print_r($param);
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());
If put the $param into the GetOrders function then, it breaks and nothing happens.
Even if I use an array in the $client->GetOrders(array('someArry' => $param)) then response and request still always looks the same and looks like the body of the SOAP request is never created:
Request:
?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://publicapi.ekmpowershop.com/"><SOAP-ENV:Body><ns1:GetOrders/></SOAP-ENV:Body></SOAP-ENV:Envelope>
Response:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetOrdersResponse xmlns="http://publicapi.ekmpowershop.com/"><GetOrdersResult><Status>Failure</Status><Errors><string>Object reference not set to an instance of an object.</string></Errors><Date>2017-04-03T16:00:42.9457446+01:00</Date><TotalOrders>0</TotalOrders><TotalCost xsi:nil="true" /></GetOrdersResult></GetOrdersResponse></soap:Body></soap:Envelope>
If anyone can shed some light on what I am doing wrong here that would be real big help?
P.S My experience of SOAP in PHP is limited as I am used to SOAP in a java env. Thanks
You need to pass the parameters into the $client->GetOrders() call. Also the WSDL defines some required parameters, so a minimal example is something like:
$wsdl = 'http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$param = array(
'GetOrdersRequest' => array(
'APIKey' => 'dummy-key',
'CustomerID' => 1,
'ItemsPerPage' => 1,
'PageNumber' => 1,
)
);
$resp = $client->GetOrders($param);
print_r($param);
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());
This gives the error response:
<Errors><string>Invalid character in a Base-64 string.</string></Errors>
which presumably is because my API key is invalid.

PHP XML Soap request

How I can do below soap request in php,
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:v="http://incometaxindiaefiling.gov.in/ditws/TaxCredMismatch/v_1_0">
<soapenv:Header/>
<soapenv:Body>
<v:getTaxCredMismatchRequest>
<LoginInfo>
<userName>XXXXXXXXXX</userName>
<password>XXXXXXXXXX</password>
</LoginInfo>
<UserInput>
<panNo>XXXXXXXXXX</panNo>
<asseessmentyear>XXXX-XX</asseessmentyear>
</UserInput>
</v:getTaxCredMismatchRequest>
</soapenv:Body>
</soapenv:Envelope>
I tried below code,
<?php
$url = "https://incometaxindiaefiling.gov.in/e-FilingWS/ditws/getTaxCredMismatchRequest.wsdl";
try {
$options = array(
'soap_version'=>SOAP_1_1,
'exceptions'=>true,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE
);
$client = new SoapClient($url,$options);
$requestParams = array(
'userName' => 'AJAPA5855E',
'password' => 'pass123',
'panNo' => 'AJAPA5855E',
'asseessmentyear' => '2014-15'
);
$response = $client->__soapCall("getTaxCredMisMatch", array($requestParams));
var_dump($response);
} catch (Exception $e) {
echo $e->getMessage();
}
?>
but getting the response as
SOAP-ERROR: Encoding: object has no 'LoginInfo' property
I know, I'm sending the parameter in correct way, may I know how to correct it.
I never used that soap client, but I would expect this:
<?php
$url = "https://incometaxindiaefiling.gov.in/e-FilingWS/ditws/getTaxCredMismatchRequest.wsdl";
try {
$options = array(
'soap_version'=>SOAP_1_1,
'exceptions'=>true,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE
);
$client = new SoapClient($url,$options);
$requestParams = array(
'LoginInfo' => array (
'userName' => 'AJAPA5855E',
'password' => 'pass123',
),
'UserInput' => array (
'panNo' => 'AJAPA5855E',
'asseessmentyear' => '2014-15'
)
);
$response = $client->__soapCall("getTaxCredMisMatch", array($requestParams));
var_dump($response);
} catch (Exception $e) {
echo $e->getMessage();
}
?>
However as said above, this is just a wild guess. It certainly would make sense to take a look at the documentation of that extension: http://php.net/manual/en/class.soapclient.php. Such things should be explained in there...
Using the WSDL from https://incometaxindiaefiling.gov.in/e-FilingWS/ditws/getTaxCredMismatchRequest.wsdl, you could generate the corresponding package from wsdltophp.com in order to be sure on how to structure your request in PHP as every element will be a PHP object with setters/getters. It uses the native PHP SoapClient class so you'll understand easily and quickly who to send these requests if you're familiar with PHP

SoapClient does not send parameters

I am pulling my hair out. I have tried many different ways, but nothing works:
<?php
// Proxy is for Fiddler
$soap = new soapClient( 'http://testi.lemonsoft.eu:22000/CTP/lemonweb/userservices.svc?wsdl', array(
'proxy_host' => 'localhost',
'proxy_port' => '8888'
));
try {
$test = new stdClass();
$test->UserName = "foo";
$test->Password = "bar";
$test->CompanyDatabase = "baz";
// This should work:
print_r($soap->LogIn($test));
/** The rest are alternative experiments, no avail: **/
print_r($soap->LogIn(array($test)));
print_r($soap->LogIn(array('parameters' => $test)));
print_r($soap->login(array(
'UserName' => 'foo',
'Password' =>'bar',
'CompanyDatabase' => 'baz'
)));
print_r($soap->__soapCall('LogIn', array('parameters' => $test)));
print_r($soap->__soapCall('LogIn', array('parameters' => array(
'UserName' => 'foo',
'Password' =>'bar',
'CompanyDatabase' => 'baz'
))));
print_r($soap->LogIn(new SoapParam($test, "LogIn")));
print_r($soap->LogIn(new SoapParam(array(
'UserName' => 'foo',
'Password' =>'bar',
'CompanyDatabase' => 'baz'
), "LogIn")));
print_r($soap->__soapCall('LogIn', array('parameters' => array(
new SoapParam(array(
'UserName' => 'foo',
'Password' =>'bar',
'CompanyDatabase' => 'baz'
), "LogIn")
))));
} catch (SoapFault $fault) {
print_r($fault);
}
?>
I captured the requests with fiddler and the response always looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body>
<ns1:LogIn/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
as if the LogIn parameters are never sent. Does the empty ns1:LogIn tag really mean that? Can there be some instance inbetween, which I can not control, for some reason stripping the parameters? According to my understanding the method LogIn takes one parameter, which, according to the documentation, should be a PHP stdClass.
Try this:
class LogInInfo{
public $UserName = '1';
public $Password = '2';
public $CompanyDatabase = '3';
}
ini_set('display_error', 1);
error_reporting(E_ALL);
$client = new SoapClient('http://testi.lemonsoft.eu:22000/CTP/LemonWeb/UserServices.svc?singleWsdl', array(
'classmap'=>array('LogInInfo'=>'LogInInfo'),
'debug'=>true,
'trace'=>true
));
try {
$info = new LogInInfo();
$resp = $client->LogIn($info);
} catch(Exception $e) {
var_dump($e);
}
print_r($client->__getLastRequest());
Result:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.datacontract.org/2004/07/Lemonsoft.LemonsoftServiceLibrary.User" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://tempuri.org/">
<SOAP-ENV:Body>
<ns2:LogIn xsi:type="ns1:LogInInfo">
<ns1:CompanyDatabase>3</ns1:CompanyDatabase>
<ns1:Password>2</ns1:Password>
<ns1:UserName>1</ns1:UserName>
</ns2:LogIn>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Can you show content of the WSDL file?
ns1:LogIn tag means that method LogIn is from ns1 namespace (defined above).
The only way I manage to pass the params in SOAP request from PHP is like this:
// __setSoapHeaders here
$listCriteriaXML = '<List xmlns="LINKHERE">
<listCriteria>
<ListCriterion>
<Name>DateNeeded</Name>
<SingleValue>' . date("Y-m-d", strtotime("+120 days")) . '</SingleValue>
</ListCriterion>
<ListCriterion>
<Name>limitresults</Name>
<SingleValue>false</SingleValue>
</ListCriterion>
</listCriteria>
</List>';
$listCriteria = new SoapVar($listCriteriaXML, XSD_ANYXML);
$response = $client->List($listCriteria);
echo $client->__getLastRequest();

PHP Soap authentication header

Sample Request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="https://ws.intermedia.net/Account/Management">
<soapenv:Header>
<AuthentificationInfo>
<login>[PLRAdminUserName]</login>
<password>[PLRAdminPassword]</password>
<accountID>[accountID]</accountID>
</AuthentificationInfo>
</soapenv:Header>
<soapenv:Body>
<GetAccount>
<accountID>[accountID]</accountID>
</GetAccount>
</soapenv:Body>
</soapenv:Envelope>
WSDL: https://controlpanel.msoutlookonline.net/WebServices/Account/AccountService.asmx?WSDL
PHP:
ini_set("soap.wsdl_cache_enabled", "0");
$wsdl = "https://controlpanel.msoutlookonline.net/WebServices/Account/AccountService.asmx?WSDL";
$ns = 'https://ws.intermedia.net/Account/Management';
$client = new SoapClient($wsdl, array(
"trace" => 1,
"exceptions" => 0
));
$login = 'xxxx';
$password = 'xxxx';
$partnerID = 1234;
$accountID = 12345678;
$headerBody = array('AuthentificationInfo'=>array(
'login' => $login,
'password' => $password,
'accountID' => $partnerID
));
$header = new SoapHeader($ns, 'AuthentificationInfo', $headerBody);
$client->__setSoapHeaders($header);
$client->__soapCall("echoVoid", array(null));
$value = $client->GetAccount($accountID);
I'm getting the following error message:
soap:ServerServer was unable to process request. ---> Access denied; Code: 0x0008
Can anyone see anything wrong with the code?
Try with
$headerBody = array(
'login' => $login,
'password' => $password,
'accountID' => $partnerID);
just for anyone else that may ever run across this:
I changed
$ns = 'https://ws.intermedia.net/Account/Management';
to:
$ns = 'http://schemas.msoutlookonline.net';
I had an incorrect namespace.
Also Mikaƫl DELSOL's answer helped as I didn't need the array('AuthentificationInfo'=> part.
Also didn't need: $client->__soapCall("echoVoid", array(null));
Thank you!

Categories