Amusons nous avec PHP, aujourd'hui : le JSON et le console

Share and options

Le petit problème du jour est le suivant : dans une application fortement AJAX, jQuery en client side, PHP en server side, j'ai besoin d'effectuer le debug de requêtes AJAX provenant du client en POST contenant du JSON, et le retour du serveur, contenant, du JSON aussi!

Pour ceci, on pourrait utiliser Firebug, que tout le monde connait bien, mais ne m'occupant pas de la partie JS, mais du code PHP serveur, j'ai pas envie d'inspecter 3000 lignes de JS pour mettre un break point au bon endroit.

Pour ceci, petit feinte, utiliser l'onglet Console de Firebug, et PHP en command line.

Imaginons, le client envoie une requête AJAX contenant du POST data en JSON. Mon script PHP le prend, exécute ce qu'il a faire avec, et envoie un retour conséquent, voici comment le lire de façon plus humaine.

Premièrement, trouver la requête dans l'onglet Console de Firebug, vous aurez, dans le sous-onglet response de cette dernière une sortie de la sorte :

{ "success": true, "containers": [ { "nature": "region", "id": "header", "allowed": [ { "nature": "nodeblocks_block", "id": "5" } ] }, { "nature": "region", "id": "content", "allowed": [ { "nature": "nodeblocks_block", "id": "5" } ] }, { "nature": "region", "id": "left", "allowed": [ { "nature": "nodeblocks_block", "id": "5" } ] }, { "nature": "region", "id": "footer", "allowed": [ { "nature": "nodeblocks_block", "id": "5" } ] }, { "nature": "bucket", "id": "1", "allowed": [ { "nature": "nodeblocks_block", "id": "5" } ] } ], "output": [  ], "registry": { "bricks_registry": [ { "options": { "access": false, "mode": "copy", "limit": null, "allowDuplicates": false, "renderElementNeeded": true, "forceSave": false }, "allowed": [  ], "children": [  ], "nature": "field", "id": "field_images_body-5" }, { "options": { "access": false, "mode": "copy", "limit": null, "allowDuplicates": false, "renderElementNeeded": true, "forceSave": false }, "allowed": [  ], "children": [  ], "nature": "field", "id": "field_attached_files-5" }, { "options": { "access": false, "mode": "copy", "limit": null, "allowDuplicates": false, "renderElementNeeded": true, "forceSave": false }, "allowed": [  ], "children": [  ], "nature": "fieldnoderef", "id": "field_references-5" }, { "options": { "access": false, "mode": "copy", "limit": null, "allowDuplicates": false, "renderElementNeeded": true, "forceSave": false }, "allowed": [  ], "children": [  ], "nature": "fieldnoderef", "id": "field_comments-5" }, { "options": { "access": false, "mode": "copy", "limit": null, "allowDuplicates": false, "renderElementNeeded": true, "forceSave": false }, "allowed": [  ], "children": [  ], "nature": "fieldnoderef", "id": "field_sources-5" } ] } }

Assez illisible, n'est-ce pas?

Le mettre en forme en deux secondes, c'est possible! Ouvez un terminal, et lancer PHP en mode console interractive, comme ceci :

pounard@guinevere ~ $ php -a
Interactive shell

php >

Maintenant, tapez à la main pour obtenir ceci :

pounard@guinevere ~ $ php -a
Interactive shell

php > print_r(json_decode('

Attention, n'oubliez pas le single quote à la fin. L'encodage JSON n'utilisera souvent que des double quote pour délimiter ses chaînes de caractères, ce qui nous arrange bien ici.

Ensuite, copiez coller la sortie brute de Firebug à la suite de ce que vous venez de tapez, et complétez avec:

', TRUE));

Puis tapez entrée. Le , TRUE ici est un paramètre de la fonction PHP json_decode qui indique de formatter le JSON décodé sous la forme d'Array, en ignorant les { et } qui sont normalement interprêtés comme des stdClass.

Enjoy!

Voici ce que l'output complet donne, avec le texte d'exemple donné ci dessus :

pounard@guinevere ~ $ php -a
Interactive shell

php > print_r(json_decode('{ "success": true, "containers": [ { "nature": "region", "id": "header", "allowed": [ { "nature": "nodeblocks_block", "id": "5" } ] }, { "nature": "region", "id": "content", "allowed": [ { "nature": "nodeblocks_block", "id": "5" } ] }, { "nature": "region", "id": "left", "allowed": [ { "nature": "nodeblocks_block", "id": "5" } ] }, { "nature": "region", "id": "footer", "allowed": [ { "nature": "nodeblocks_block", "id": "5" } ] }, { "nature": "bucket", "id": "1", "allowed": [ { "nature": "nodeblocks_block", "id": "5" } ] } ], "output": [  ], "registry": { "bricks_registry": [ { "options": { "access": false, "mode": "copy", "limit": null, "allowDuplicates": false, "renderElementNeeded": true, "forceSave": false }, "allowed": [  ], "children": [  ], "nature": "field", "id": "field_images_body-5" }, { "options": { "access": false, "mode": "copy", "limit": null, "allowDuplicates": false, "renderElementNeeded": true, "forceSave": false }, "allowed": [  ], "children": [  ], "nature": "field", "id": "field_attached_files-5" }, { "options": { "access": false, "mode": "copy", "limit": null, "allowDuplicates": false, "renderElementNeeded": true, "forceSave": false }, "allowed": [  ], "children": [  ], "nature": "fieldnoderef", "id": "field_references-5" }, { "options": { "access": false, "mode": "copy", "limit": null, "allowDuplicates": false, "renderElementNeeded": true, "forceSave": false }, "allowed": [  ], "children": [  ], "nature": "fieldnoderef", "id": "field_comments-5" }, { "options": { "access": false, "mode": "copy", "limit": null, "allowDuplicates": false, "renderElementNeeded": true, "forceSave": false }, "allowed": [  ], "children": [  ], "nature": "fieldnoderef", "id": "field_sources-5" } ] } }', TRUE));
Array
(
    [success] => 1
    [containers] => Array
        (
            [0] => Array
                (
                    [nature] => region
                    [id] => header
                    [allowed] => Array
                        (
                            [0] => Array
                                (
                                    [nature] => nodeblocks_block
                                    [id] => 5
                                )

                        )

                )

            [1] => Array
                (
                    [nature] => region
                    [id] => content
                    [allowed] => Array
                        (
                            [0] => Array
                                (
                                    [nature] => nodeblocks_block
                                    [id] => 5
                                )

                        )

                )

            [2] => Array
                (
                    [nature] => region
                    [id] => left
                    [allowed] => Array
                        (
                            [0] => Array
                                (
                                    [nature] => nodeblocks_block
                                    [id] => 5
                                )

                        )

                )

            [3] => Array
                (
                    [nature] => region
                    [id] => footer
                    [allowed] => Array
                        (
                            [0] => Array
                                (
                                    [nature] => nodeblocks_block
                                    [id] => 5
                                )

                        )

                )

            [4] => Array
                (
                    [nature] => bucket
                    [id] => 1
                    [allowed] => Array
                        (
                            [0] => Array
                                (
                                    [nature] => nodeblocks_block
                                    [id] => 5
                                )

                        )

                )

        )

    [output] => Array
        (
        )

    [registry] => Array
        (
            [bricks_registry] => Array
                (
                    [0] => Array
                        (
                            [options] => Array
                                (
                                    [access] =>
                                    [mode] => copy
                                    [limit] =>
                                    [allowDuplicates] =>
                                    [renderElementNeeded] => 1
                                    [forceSave] =>
                                )

                            [allowed] => Array
                                (
                                )

                            [children] => Array
                                (
                                )

                            [nature] => field
                            [id] => field_images_body-5
                        )

                    [1] => Array
                        (
                            [options] => Array
                                (
                                    [access] =>
                                    [mode] => copy
                                    [limit] =>
                                    [allowDuplicates] =>
                                    [renderElementNeeded] => 1
                                    [forceSave] =>
                                )

                            [allowed] => Array
                                (
                                )

                            [children] => Array
                                (
                                )

                            [nature] => field
                            [id] => field_attached_files-5
                        )

                    [2] => Array
                        (
                            [options] => Array
                                (
                                    [access] =>
                                    [mode] => copy
                                    [limit] =>
                                    [allowDuplicates] =>
                                    [renderElementNeeded] => 1
                                    [forceSave] =>
                                )

                            [allowed] => Array
                                (
                                )

                            [children] => Array
                                (
                                )

                            [nature] => fieldnoderef
                            [id] => field_references-5
                        )

                    [3] => Array
                        (
                            [options] => Array
                                (
                                    [access] =>
                                    [mode] => copy
                                    [limit] =>
                                    [allowDuplicates] =>
                                    [renderElementNeeded] => 1
                                    [forceSave] =>
                                )

                            [allowed] => Array
                                (
                                )

                            [children] => Array
                                (
                                )

                            [nature] => fieldnoderef
                            [id] => field_comments-5
                        )

                    [4] => Array
                        (
                            [options] => Array
                                (
                                    [access] =>
                                    [mode] => copy
                                    [limit] =>
                                    [allowDuplicates] =>
                                    [renderElementNeeded] => 1
                                    [forceSave] =>
                                )

                            [allowed] => Array
                                (
                                )

                            [children] => Array
                                (
                                )

                            [nature] => fieldnoderef
                            [id] => field_sources-5
                        )

                )

        )

)
php >