user3359899 user3359899 - 4 months ago 25
JSON Question

PHP json_decode error - Mandrill Webhook

Im trying to handle Mandrills webhook data.
I will get a $_POST request with some Json Data, while handling these data i got a strange problem.

If I decode the Data given by POST, I just get an empty var :(
Any idea why?

EDIT:

the strangest thing ever ... If I use the Json directly it doesnt work, but if I insert it into the Database and take it from there, it works perfectly

$json = $_POST['mandrill_events'];
print_r(json_decode($json,true));
//False

$data['content'] = $_POST['mandrill_events'];
$id = $db->insert('mail', $data);
$get = $db->select('content', 'mail', 'id = "'.(int) $id.'"'); //= mysql_fetch_array($data,MYSQL_ASSOC)
$json = $get['content'];
print_r(json_decode($json,true));
//True


Old Code

/*
print_r($_POST)

Array
(
[mandrill_events] => [{"event":"inbound","ts":1393490345,"msg":{"raw_msg":"Received: from"}}]
(shortend)
)
*/

$test_var = '[{"event":"inbound","ts":1393490345,"msg":{"raw_msg":"Received: from"}}]';

$getMessage = $_POST['mandrill_events'];
print_r($getMessage);
//Until now everything works fantastic
$getMessage = json_decode($getMessage,true);
print_r($getMessage);
//Nothing - the output is empty ;(

$getTestMessage = json_decode($test_var,true);
print_r($getTestMessage);
//works fine

Answer

Use stripslashes. In your case:

$json = $_POST['mandrill_events'];
print_r(json_decode(stripslashes($json),true));
Comments