2009年3月20日金曜日

(FreeBSD) PHPのXMLRPCで、9.99999E-99みたいなdouble値がXML_RPC_decodeで変になる

phpのせいかもしれないし、pear-XML_RPCのせいかもしれないし、もしかすると、以前の

(FreeBSD) PHPのXMLRPCを使うと、オブジェクトをserializeしたテキスト中の「"」が欠落するようになった

と同じようにlibxml2-2.7.2関係かもしれないし、ぜんぜん問題の切り分けはやってないんですが・・・



たとえば、1.23456E-7という値をサーバーから受け取ったとき、XML_RPC_decodeすると



<pre>---GOT---
HTTP/1.1 200 OK
Date: Wed, 18 Mar 2009 05:08:44 GMT
Server: Apache/2.2.11 (FreeBSD) mod_ssl/2.2.11 OpenSSL/0.9.8j DAV/2 PHP/5.2.8 with Suhosin-Patch SVN/1.5.5
X-Powered-By: PHP/5.2.8
Content-Length: 152
Connection: close
Content-Type: text/xml; charset=UTF-8

<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value><double>1.23456E-7</double></value>
</param>
</params>
</methodResponse>
---END---</pre>

<pre>---PARSED---
object(XML_RPC_Value)#6 (2) {
  ["me"]=>
  array(1) {
    ["double"]=>
    int(102)

  }
  ["mytype"]=>
  int(1)
}
---END---</pre>



なぜか102という整数値になってしまいました。



doubleだとダメっていうわけでもなく、1.23456なら



<pre>---GOT---
HTTP/1.1 200 OK
Date: Wed, 18 Mar 2009 05:02:55 GMT
Server: Apache/2.2.11 (FreeBSD) mod_ssl/2.2.11 OpenSSL/0.9.8j DAV/2 PHP/5.2.8 with Suhosin-Patch SVN/1.5.5
X-Powered-By: PHP/5.2.8
Content-Length: 149
Connection: close
Content-Type: text/xml; charset=UTF-8

<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value><double>1.23456</double></value>
</param>
</params>
</methodResponse>
---END---</pre>

<pre>---PARSED---
object(XML_RPC_Value)#6 (2) {
  ["me"]=>
  array(1) {
    ["double"]=>
   
string(7) "1.23456"
  }

  ["mytype"]=>
  int(1)
}
---END---</pre>


こっちはよさそう・・・なぜかstringっていう文字が見えてるけど、ま、いいか。



じゃあstringにしてしまえ

$value = new XML_RPC_Value((string)$tmp, "string");

<pre>---GOT---
HTTP/1.1 200 OK
Date: Wed, 18 Mar 2009 05:13:36 GMT
Server: Apache/2.2.11 (FreeBSD) mod_ssl/2.2.11 OpenSSL/0.9.8j DAV/2 PHP/5.2.8 with Suhosin-Patch SVN/1.5.5
X-Powered-By: PHP/5.2.8
Content-Length: 152
Connection: close
Content-Type: text/xml; charset=UTF-8

<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value><string>1.23456E-7</string></value>
</param>
</params>
</methodResponse>
---END---</pre>

<pre>---PARSED---
object(XML_RPC_Value)#6 (2) {
  ["me"]=>
  array(1) {
    ["string"]=>
    string(10) "1.23456E-7"

  }
  ["mytype"]=>
  int(1)
}
---END---</pre>



これは当然動くんだけど、なんか違うぞ!って思います。





php 5.2.9へportupgradeしてみたけど、変化なし。



<pre>---GOT---
HTTP/1.1 200 OK
Date: Wed, 18 Mar 2009 23:12:09 GMT
Server: Apache/2.2.11 (FreeBSD) mod_ssl/2.2.11 OpenSSL/0.9.8j DAV/2 PHP/5.2.9 with Suhosin-Patch SVN/1.5.5
X-Powered-By: PHP/5.2.9
Content-Length: 152
Connection: close
Content-Type: text/xml; charset=UTF-8

<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value><double>1.23456E-7</double></value>
</param>
</params>
</methodResponse>
---END---</pre>

<pre>---PARSED---
object(XML_RPC_Value)#6 (2) {
  ["me"]=>
  array(1) {
    ["double"]=>
    int(102)

  }
  ["mytype"]=>
  int(1)
}
---END---</pre>
result=102


それとも、これはバグではなくて、単に使い方を勘違いしてる?!



0 件のコメント:

コメントを投稿