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

时间:2011-08-19 19:03:59

标签: php soap-client scientific-notation

我正在使用PHP SoapClient并将信用卡号作为其中一个值传递。 soapCall方法将此数字转换为指数格式。我正在通过4321432143274321,服务器正在<ccNumber xsi:type="xsd:double">4.3214321432743E+15</ccNumber>。我已经尝试将值转换为double和string无效。

我无法在任何地方找到任何有关此问题的提示!我找到的最接近的是: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] => 
    )    

最后,服务器正在看到肥皂调用部分:

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

3 个答案:

答案 0 :(得分:2)

我找到了解决方案!

我更新了php.ini文件中的“precision”设置。我将该值从14增加到16.我需要信用卡号码的最大值是16位数。相应地调整您的需求!

答案 1 :(得分:0)

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

[ccNumber] => 4321432143274321 . ""

答案 2 :(得分:0)

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

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