Question

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
Was it helpful?

Solution

Use stripslashes. In your case:

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

OTHER TIPS

I have implemented like this it can be working fine to me plz check this one

      $data = $_POST;

    $restult_data_decode = json_decode($data['mandrill_events']);


   // print_r($restult_data_decode);
    //exit;




    $message_id     =  $restult_data_decode[0]->_id;

    $status         =  $restult_data_decode[0]->msg->state;

    $reject_reason  =  $restult_data_decode[0]->msg->reject;


    echo "message_id............ $message_id  .............status:$status..........reject reason : $reject_reason ";

You probably have magic_quotes turned on on your server. This should work:

 $events = json_decode(stripslashes($_POST['mandrill_events']),1);
 print_r($events);

When you print POST data it echos as html. May be in your POST Data there are " or " are coming inplace of double Quotes. Check that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top