0

I'm trying to collect my form data and to send is as json by curl to my server. I'm stuck constructing the JSON object. here is what I get for var_dump($_POST):

Array array(16) {
["id"]=> string(1) "5" 
["lastMade"]=> string(19) "08-20-2015 21:00:17" 
["integrationType"]=> string(11) "EMAILREPORT" 
["active"]=> string(6) "active" 
["tableName"]=> string(18) "affiliates_revenue" 
["projectId"]=> string(6) "114133" 
["description"]=> string(18) "affiliates_revenue"
 ...

}

When I call var_dump for var_dump(json_decode($_POST)):

NULL

I tried to use stripslashes() but it still returned NULL.

Thanks,

  • 4
    you want to convert right array to json right. Why use json_decode. Please use json_encode – Faiz Aug 20 '15 at 18:19
  • 3
    As $POST is an array in PHP (as your `var_dump($_POST)` is telling you), and not a json encoded string, calling `json_decode()` on it will fail. Perhaps you meant `json_encode()` – Mark Baker Aug 20 '15 at 18:19
  • @MarkBaker's suggestion of `json_encode()` will work most of the time, but be wary of encodings - you cannot guarantee your POST data will be UTF-8 encoded. – George Brighton Aug 20 '15 at 18:27
  • I'm not sure what your purpose is for turning $_POST into json, but you probably want [`json_encode($_POST)`][1] to convert to a json string. (Standard caveats around sanitizing user input apply.) [`json_decode()`][2] creates an object (or array) from a json-formatted string. Call `json_last_error()` and you'll see that your code fails because you have not provided a valid json string. If you just want to convert the $_POST to an object, use a simple cast: ```$post_object = (object)$_POST;``` [1]: http://php.net/json_encode [2]: http://php.net/json_decode – aaronbauman Aug 23 '18 at 19:53

1 Answers1

-1

You need first encode and then decode: json_decode(json_encode($_POST));

rgfpy
  • 15
  • 3