Veltu Posted November 6, 2014 Share Posted November 6, 2014 Hi all, I'm posting JSON to an API call. If I use a var_dump within the API it will return correctly, although if I try to var_dump within the function it will return NULL. API: $json = file_get_contents('php://input'); if(isset($_GET['form_id']) && intval($_GET['form_id'])>0) { if(isset($json)) { $records = $questionnaire->logFormAnswers($_POST['answers'],$_GET['form_id']); Then using the logFormAnswers function... Function: public function logFormAnswers($answers = "", $form_id = 0) { $answers = json_decode($json,true); var_dump($answers); The JSON: [{"question_id":"3","answer":"dsf"},{"question_id":"3","answer":"sdfsdfdsfsd"}] Eventually, once the JSON has been decoded, it will be inserted into a database within the function. If anyone can see what I'm doing wrong, any help is appreciated, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/292312-json-decode-returning-null/ Share on other sites More sharing options...
Solution Barand Posted November 6, 2014 Solution Share Posted November 6, 2014 $json is not defined within the function. You need to pass it as an additional argument. Quote Link to comment https://forums.phpfreaks.com/topic/292312-json-decode-returning-null/#findComment-1495904 Share on other sites More sharing options...
Veltu Posted November 6, 2014 Author Share Posted November 6, 2014 Thank you Barand, problem solved and I can now insert my data into the table! Quote Link to comment https://forums.phpfreaks.com/topic/292312-json-decode-returning-null/#findComment-1495914 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.