PHP 5.6 SoapClient 传递十进制错误

PHP 5.6 SoapClient passing decimal bug

我正在尝试使用 SoapClient 函数将 PHP 数组解析为 SOAP XML。 我必须将十进制数解析为 SOAP。我的 PHP 数组看起来像这样:

$contactparams = array(

"DECIMALVARIABLE"=>0.00

);

执行 SOAP 调用时出现错误,我应该使用带 2 位小数的十进制数。我猜 PHP 函数将 0.00 转换为纯 0.

抛出的错误示例:

Fatal error: Uncaught SoapFault exception: [soap:Client] Invalid number 2dp value (0)

当我使用 0.01 时,它工作得很好。

我已经尝试将其解析为字符串“0.00”和 0.00。”

我也试过 number_format 函数来创建 0.00 但结果相同。

任何解决这个谜语的想法都将不胜感激。

这个卑鄙的把戏可以帮助您了解这个错误是否真的与您的十进制变量有关

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

“shortnumber2dp”类型的字段必须带有 2 位小数:0.00 而不是 0

确保您没有在 number_format

中传递空字符串
echo "A. ".number_format(null, 2)."\n";
// output 0.00
echo "B. ".number_format(0, 2)."\n";
// output 0.00
echo "C. ".number_format('', 2)."\n";
// error

解决这个问题

if(empty($value)){$value = 0;}

经过几个小时的寻找解决方案,我终于找到了一个狡猾的解决方案...

当输入值 0.001 时,SOAP 的函数会将其更改为正确的值 0.00

所以:

$contactparams = array(

"DECIMALVARIABLE"=>0.001

);

给予:

DECIMALVALUE=0.00

不是一个干净的解决方案,但可以工作..