开发者

How Can I Prevent PHP SoapClient From Converting Double Value to Scientific Notation?

开发者 https://www.devze.com 2023-03-29 14:59 出处:网络
I\'m using the PHP SoapClient and passing a credit card number as one of the values.This number is getting converted to exponential format by the soapCall method.I\'m passing 4321432143274321 and the

I'm using the PHP SoapClient and passing a credit card number as one of the values. This number is getting converted to exponential format by the soapCall method. I'm passing 4321432143274321 and the server is getting <ccNumber xsi:type="xsd:double">4.3214321432743E+15<开发者_Go百科/ccNumber>. I've tried casting the value as a double and a string to no avail.

I cannot find any hints on this issue anywhere! The closest I have found is this: http://www.php.net/manual/en/soapclient.soapcall.php#75078, but the "fix" isn't really a fix.

Here's my code:

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

Here's the $arguments array being passed:

    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] => 
    )    

And finally the section of the soap call the server is seeing:

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


I found the solution!

I updated the "precision" setting in the php.ini file. I increased that value from 14 to 16. Sixteen digits is the max I need for credit card numbers. Adjust to your needs accordingly!


Cast your ccNumber to a string to prevent serialization from using the floating point representation.

[ccNumber] => 4321432143274321 . ""


You can use the number_format function to convert the number to string exactly. By default, it uses a comma as thousands separator. To avoid this, you have to explicitly override it:

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

精彩评论

暂无评论...
验证码 换一张
取 消