I'm trying to parse an PHP array to SOAP XML with the SoapClient function. I have to parse a decimal number to SOAP. My PHP array looks lik this:
$contactparams = array(
"DECIMALVARIABLE"=>0.00
);
When executing the SOAP call I'm getting the error thrown that I should use a decimal number with 2 decimals. My guess it that the PHP function converts the 0.00 to plain 0.
Example of the error thrown:
Fatal error: Uncaught SoapFault exception: [soap:Client] Invalid number 2dp value (0)
When I use 0.01 it works just fine.
I've already tried to parse it as a string "0.00" & 0.00.""
I've also tried the number_format function to create the 0.00 but with the same result.
Any ideas how to solve this riddle would be greatly appreciated.
After hours of searching for a solution I've finally found a devious one...
When entering the value 0.001 SOAP's functions changes this to the correct value of 0.00
SO:
$contactparams = array(
"DECIMALVARIABLE"=>0.001
);
GIVES:
DECIMALVALUE=0.00
Not quite a clean solution, but one that works..
this dirty trick can help you understand is this error really related to you decimal variable
<?php
class MySoapClient extends SoapClient
{
public $decimalValue;
function __doRequest($request, $location, $action, $version) {
if (is_null($this->decimalValue)
throw new Exception("you forgot to set decimalValue");
$request = str_replace('#decimalValuePlaceHolder#', $this->decimalValue, $request);
$ret = parent::__doRequest($request, $location, $action, $version);
$this->__last_request = $request;
return $ret;
}
}
// usage
$contactparams = array(
"DECIMALVARIABLE"=>'#decimalValuePlaceHolder#',
);
$client = new MySoapClient(); // initialize this as prev SoapClient
$client->decimalValue = "0.00"; // or number_format($someNumber, 2); // any decimal variable
$client->callFunction(); // call you function as you did before
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With