I have a function that returns a json encoded response from a $_POST:
function getdatafrompost(){
$raw = '';
$httpContent = fopen('php://input', 'r');
while ($kb = fread($httpContent, 1024))
$raw .= $kb;
fclose($httpContent);
$params = array();
parse_str($raw, $params);
if (isset($params['data']))
$retdata = json_decode(stripslashes($params['data']));
else
$retdata = json_decode(stripslashes($raw)); <====Returns Null
return $retdata;
}
The raw data being read is:
{"recordid":"099ac8aa-0a43-11e2-860f-0016177c526f","ntsdataid":"","trkdataid":"5628af8e-08db-11e2-860f-0016177c526f","activitydate":"2012-09-29","activitytime":"10:36:21","activityname":" from S. Interrante","activitynote":"<i><font color=\"99CC00\" size=\"4\"><b><font face=\"verdana\">fgsdfgsdgsdfgsdgsdgsdfg</font>\u200b</b></font></i>"}
The problem is $retdata is returning null. This appears to be in the “activitynote” field, which contains HTML. When “activitynote” contains no HTML then data is parsed properly. Is there a better approach to parsing the data? Am I doing something incorrectly?
You are not supposed to use
stripslashes, it would affect the validity of the json document to fix this error you should replaceWith
Example
Output