Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 5.6 SoapClient passing decimal bug

Tags:

php

soap

decimal

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.

like image 340
Joeri Minnekeer Avatar asked May 02 '16 08:05

Joeri Minnekeer


2 Answers

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..

like image 170
Joeri Minnekeer Avatar answered Sep 30 '22 20:09

Joeri Minnekeer


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
like image 38
Denis Alimov Avatar answered Sep 30 '22 20:09

Denis Alimov