php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48028 Can't encode struct with integer name
Submitted: 2009-04-20 16:26 UTC Modified: 2009-04-21 10:07 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: php at atis dot id dot lv Assigned:
Status: Not a bug Package: XMLRPC-EPI related
PHP Version: 5.2.9 OS: Linux 2.6.24.7-92.fc8 x86_64
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php at atis dot id dot lv
New email:
PHP Version: OS:

 

 [2009-04-20 16:26 UTC] php at atis dot id dot lv
Description:
------------
It is impossible to generate <struct> responses from associative arrays with numeric and non-zero-based keys.

I was hoping to see either:

* php string keys are converted to <struct> even if they have numeric value. 
Example: array('123'=>'abc') becomes 

<struct><name>123</name><value>abc</value></struct>

* all non-zero-based and non-sequential array keys are converted to <struct>.

For example:

array('a','b','c') becomes:
<array><value>a</value><value>b</value><value>c</value></array>

array('a',3=>'b','5'=>'c') becomes:
<struct>
  <name>0</name><value>a</value>
  <name>3</name><value>b</value>
  <name>5</name><value>c</value>
</struct>

* output option that would automatically convert all arrays to <struct> type. 

I was looking into extension source, and found that within determine_vector_type and PHP_to_XMLRPC_worker a variable and test with simple 0 based index should be simple enough, however somehow zend_hash_get_current_key doesn't detect the initial type of array key correctly. Of course converting it to *char would be possible, but i'm not sure if it's the correct way.


Workarounds found in comments of bug #37746 is to process return array in recursive way and add chr(0) at end of each numeric key, but that is ugly and slow. Example for workaround:

  function xmlrpc_fix_numeric_keys($data) {
    $result = array();
    if (!is_array($data)) return $data;
    foreach ($data as $k=>$v) {
      if (is_numeric($k)) $k=$k.chr(0);
      $result[$k]=xmlrpc_fix_numeric_keys($v);
    }
    return $result;
  }



Reproduce code:
---------------
  $test = array('333'=>'ddd','9212'=>'asd');
  $rpc = xmlrpc_encode($test);
  echo $rpc;



Expected result:
----------------
<?xml version="1.0" encoding="utf-8"?>
<params>
<param>
 <value>
  <struct>
   <member>
    <name>333</name>
    <value>
     <string>ddd</string>
    </value>
   </member>
   <member>
    <name>9212</name>
    <value>
     <string>asd</string>
    </value>
   </member>
  </struct>
 </value>
</param>
</params>


Actual result:
--------------
<?xml version="1.0" encoding="utf-8"?>
<params>
<param>
 <value>
  <array>
   <data>
    <value>
     <string>ddd</string>
    </value>
    <value>
     <string>asd</string>
    </value>
   </data>
  </array>
 </value>
</param>
</params>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-21 08:26 UTC] jani@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

Same as bug #37746
 [2009-04-21 10:07 UTC] php at atis dot id dot lv
I was unable to comment it, but I have feeling that You don't understand the point.

XMLRPC-EPI doesn't allow to encode <struct> with numbers in name field. This is possible in other implementations I have worked with, and the appending char(0) method is a hack that might break.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC