如何防止PHP SoapClient将双精度值转换为科学记数法?



我使用PHP SoapClient并传递信用卡号码作为其中一个值。这个数字通过soapCall方法被转换成指数格式。我正在传递4321432143274321,服务器正在获得<ccNumber xsi:type="xsd:double">4.3214321432743E+15</ccNumber>。我试过将值转换为双精度和字符串,但无济于事。

我找不到任何关于这个问题的提示!我找到的最接近的是:http://www.php.net/manual/en/soapclient.soapcall.php#75078,但"修复"并不是真正的修复。

下面是我的代码:

    private function SetUpClient()
    {
        try
        {
            $options = array( 
                'soap_version'=>SOAP_1_2, 
                'exceptions'=>true, 
                'trace'=>1, 
                'cache_wsdl'=>WSDL_CACHE_NONE
            );
            $auth->username = $this->_username;
            $auth->password = $this->_password;
            $authValues = new SoapVar($auth, SOAP_ENC_OBJECT);
            $header =  new SoapHeader("http://v1.webservices", "authentication", $authValues, false);
            $this->_soapClient = new SoapClient( $this->_wsdlUrl );
            $this->_soapClient->__setSoapHeaders(array($header));
        }
        catch( Exception $ex )
        {
            var_dump($ex);
            return "An error occured: " .  $ex->getMessage();
        }
    }
    public function CallWebService($methodName, $arguments)
    {
        try {
            $result = $this->_soapClient->__soapCall($methodName, $arguments);
        } 
        catch (Exception $e) {
            echo $e->getMessage();
            $result = false;
        }
        return $result;
    }

下面是传递的$arguments数组:

    Array
    (
            [memberUid] => 123456789
            [items] => 1809,1,N,1821,1,N
            [shippingOption] => 40
            [name1] => Test15, Juan
            [name2] => 
            [address1] => 123 Test Street
            [address2] => 
            [address3] => 
            [address4] => 
            [city] => Anytown
            [stateCode] => NY
            [countryCode] => USA
            [postalCode] => 12345
            [subOrderType] => 041
            [entryDate] => 08/18/2011
            [ccType] => 3
            [ccHolder] => Juan Test
            [ccNumber] => 4321432143274321
            [ccExpDate] => 01/2014
            [externalOrderNumber] => 
            [orderSource] => 
    )    

最后是服务器看到的soap调用部分:

    <ns1:createOrder>
        <memberUid xsi:type="xsd:string">123456789</memberUid>
        <items xsi:type="xsd:string">1809,1,N,1821,1,N</items>
        <shippingOption xsi:type="xsd:double">40</shippingOption>
        <name1 xsi:type="xsd:string">Test, Juan</name1>
        <name2 xsi:type="xsd:string"/>
        <address1 xsi:type="xsd:string">123 Test Street</address1>
        <address2 xsi:type="xsd:string"/>
        <address3 xsi:type="xsd:string"/>
        <address4 xsi:type="xsd:string"/>
        <city xsi:type="xsd:string">Anytown</city>
        <stateCode xsi:type="xsd:string">NY</stateCode>
        <countryCode xsi:type="xsd:string">USA</countryCode>
        <postalCode xsi:type="xsd:string">12345</postalCode>
        <subOrderType xsi:type="xsd:string">041</subOrderType>
        <entryDate xsi:type="xsd:string">08/19/2011</entryDate>
        <ccType xsi:type="xsd:double">3</ccType>
        <ccHolder xsi:type="xsd:string">Juan Test</ccHolder>
        <ccNumber xsi:type="xsd:double">4.3214321432743E+15</ccNumber>
        <ccExpDate xsi:type="xsd:string">01/2014</ccExpDate>
        <externalOrderNumber xsi:type="xsd:string"/>
        <orderSource xsi:type="xsd:string"/>
    </ns1:createOrder>

我找到解决方案了!

我更新了php.ini文件中的"precision"设置。我把这个值从14增加到16。我最多需要16位的信用卡号码。相应地调整你的需求!

将ccNumber转换为字符串,以防止序列化使用浮点表示。

[ccNumber] => 4321432143274321 . ""

您可以使用number_format函数将数字精确地转换为字符串。默认情况下,它使用逗号作为千位分隔符。为了避免这种情况,您必须显式地覆盖它:

$ccNumber = number_format( 4321432143274321, 0, '', '' );

相关内容

  • 没有找到相关文章

最新更新