PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Web Services » XML_RPC » Bug #9375

XML_RPC_decode decodes integer as strings in an array

Details

Submitted2006-11-18 12:28 UTC
Fromsylvain at jamendo dot com
Assigneddanielc
StatusNo Feedback
PackageXML_RPC
PHP Version5.2.0 RC4
Roadmaps(Not assigned)

Comments

[2006-11-18 12:28 UTC] sylvain at jamendo dot com

Description:
------------
With 1.5.1, XML_RPC_decode(array(int(3))) = array("3") as a string.

I know "php is a typeless language" etc.. but this breaks APIs that are used by other XML_RPC implementations in languages like Python that are typed.

Test script:
---------------
$msg = new XML_RPC_Message("dummymethod");

$rep = $msg->parseResponse('HTTP/1.1 200 OK
Date: Sat, 18 Nov 2006 11:00:57 GMT
Server: Apache/2.2.3 (Unix)
Connection: close
Content-Type: text/xml; charset=UTF-8.'."\r\n\r\n".'<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value><array>
<data>
<value><int>42</int></value>
</data>
</array></value>
</param>
</params>
</methodResponse>');

$p=$rep->value();
$d=XML_RPC_decode($p);
echo gettype($d[0]);

Expected result:
----------------
int

Actual result:
--------------
string

[2007-02-01 11:42 UTC] siudak at xz dot pl

Well, this function is quite useful when it actually works.
Here is my patch, maybe somebody will find it useful.

Index: RPC.php
===================================================================
RCS file: /repository/pear/XML_RPC/RPC.php,v
retrieving revision 1.101
diff -u -r1.101 RPC.php
--- RPC.php 28 Oct 2006 16:42:34 -0000 1.101
+++ RPC.php 1 Feb 2007 11:30:38 -0000
@@ -1972,8 +1972,14 @@
$kind = $XML_RPC_val->kindOf();

if ($kind == 'scalar') {
- return $XML_RPC_val->scalarval();
-
+ switch ($XML_RPC_val->scalartyp()) {
+ case 'int':
+ return (int)$XML_RPC_val->scalarval();
+ case 'double':
+ return (float)$XML_RPC_val->scalarval();
+ default:
+ return $XML_RPC_val->scalarval();
+ }
} elseif ($kind == 'array') {
$size = $XML_RPC_val->arraysize();
$arr = array();

[2007-02-01 13:03 UTC] siudak at xz dot pl

OK, I forgot booleans.
do it would go like this

Index: RPC.php
===================================================================
RCS file: /repository/pear/XML_RPC/RPC.php,v
retrieving revision 1.101
diff -u -r1.101 RPC.php
--- RPC.php 28 Oct 2006 16:42:34 -0000 1.101
+++ RPC.php 1 Feb 2007 12:01:18 -0000
@@ -1970,10 +1970,19 @@
function XML_RPC_decode($XML_RPC_val)
{
$kind = $XML_RPC_val->kindOf();

if ($kind == 'scalar') {
- return $XML_RPC_val->scalarval();
-
+ switch ($XML_RPC_val->scalartyp()) {
+ case 'int':
+ return (int)$XML_RPC_val->scalarval();
+ case 'double':
+ return (float)$XML_RPC_val->scalarval();
+ case 'boolean' :
+ return (bool)$XML_RPC_val->scalarval();
+ default:
+ return $XML_RPC_val->scalarval();
+ }
} elseif ($kind == 'array') {
$size = $XML_RPC_val->arraysize();
$arr = array();