views:

172

answers:

2

Take the following JSON string (generated by some ExtJS code - but that's irrelevant):

[{"action":"Setting","method":"toggle","data":["welcome-home"],"type":"rpc","tid":2},{"action":"ContentExtFeFillout","method":"todo","data":[true,0,8,false],"type":"rpc","tid":3}]

being sent to a server as a POST request and retrieved via $GLOBALS['HTTP_RAW_POST_DATA'].

Running

json_decode($GLOBALS['HTTP_RAW_POST_DATA']);

on our development machine (5.2.10-2ubuntu6.4 with Suhosin Patch 0.9.7) gives a correct print_r() output of:

Array
(
    [0] => stdClass Object
        (
            [action] => Setting
            [method] => toggle
            [data] => Array
                (
                    [0] => welcome-home
                )

            [type] => rpc
            [tid] => 2
        )

    [1] => stdClass Object
        (
            [action] => ContentExtFeFillout
            [method] => todo
            [data] => Array
                (
                    [0] => 1
                    [1] => 0
                    [2] => 8
                    [3] =>
                )

            [type] => rpc
            [tid] => 3
        )
)

Running the same code on a client's production machine (5.2.5 with Suhosin Patch 0.9.6.2 and Zend Optimizer; SUSE Linux by the way) gives the following print_r() output:

Array
(
    [0] => stdClass Object
        (
            [action] => Setting
            [method] => toggle
            [data] => Array
                (
                    [0] => welcome-home
                )

            [type] => rpc
        )

    [1] => 2
    [2] => stdClass Object
        (
            [action] => ContentExtFeFillout
            [method] => todo
            [data] => Array
                (
                    [0] => 1
                    [1] => 0
                    [2] => 8
                    [3] => 
                )

            [type] => rpc
        )

    [3] => 3
)

Note the missing tid property which obviously has been moved into the main array as an own value - this naturally breaks all the following code.

We also downloaded a Windows PHP version 5.2.5 to check if there's a bug in json_decode() but we get the correct output here.

Are there any known issues with json_decode() at all that could cause this odd behavior?

We're currently totally clueless...

Thanks to all of you!

Best regards

Stefan

A: 

Have you tried swapping the tid and type keys? Also how about using a hardcoded variable to check if the problem could be with $GLOBALS['HTTP_RAW_POST_DATA']?

Try the following:

$t1='[{"action":"Setting","method":"toggle","data":["welcome-home"],"type":"rpc","tid":2},{"action":"ContentExtFeFillout","method":"todo","data":[true,0,8,false],"tid":2,"type":"rpc"}]';
print_r(json_decode($t1));
zaf
It's quite difficult as we don't have direct access to the machine (it's an enterprise internal machine) - so we can only use logging and they'll send us the log files by mail. I might by tempted to exclude a problem with `$GLOBALS['HTTP_RAW_POST_DATA']` because what we actually do is: `log($GLOBALS['HTTP_RAW_POST_DATA'])`, `$data = json_decode($GLOBALS['HTTP_RAW_POST_DATA'])` and `log(print_r($data, true))`. The JSON string above is the exact string that has been logged - and so is the output of `print_r($data, true)`.
Stefan Gehrig
+1  A: 

OK guys - problem solved. Lacking any more options we persuaded the client to update the installed PHP version and guess what: it works.

There seems to have been a subtle bug in their PHP installation (PHP, Zend Optimizer and/or Suhosin) which has been fixed with the update. Still, a quite weird thing.

Thanks to all of you!

Best regards

Stefan

Stefan Gehrig