SzRaPnEL
SzRaPnEL

Reputation: 171

JSON format lost in PHP

When I send json data from action script 3 to php using URLVariables, the json string changes and cannot be used as json inside php. How to prevent this happening? Or how to fix it?

trace from Flash(send moethod POST, variable name myObject):

[{"data1":"value1","data2":"value2",...},{...},...]

echo $_POST['myObject'] from PHP:

[{\"data1\":\"value1\",\"data2\":\"value2\",...},{...},...]

echo json_decode($_POST['myObject']) from PHP is nothing, when var_dump(json_decode($_POST['myObject']):

NULL

Upvotes: 0

Views: 395

Answers (1)

Kovge
Kovge

Reputation: 2019

The server automatically escape the POST data (As I remember it is an option in php.ini). To unescape , use stripslashes function, and after decode your string ;)

 json_decode(stripslashes($_POST['myObject']));

Based on @therefromhere 's comment, a better solution to set magic_quotes_gpc off. You can do this if you have a root access for the server, or you have permission to set php flags at runtime. Here is some help for this: http://php.net/manual/en/security.magicquotes.disabling.php

Based on @nl-x 's comment if you want to solve this problem, undepended from your server configuration:

$myObject = get_magic_quotes_gpc() ? //Examine: is magic quotes gpc on?
               stripslashes($_POST['myObject']) : //if true: unescape the string
               $_POST['myObject'];                //if false, do nothing
json_decode($myObject);
//When php 5.3 or earlier installed on server

Upvotes: 4

Related Questions