cesarbmrg Posted July 25, 2013 Share Posted July 25, 2013 Hi, I receive this information from mandril webhook: POST /n1rhtpn1 HTTP/1.1X-Mandrill-Signature: nV34UwrCtKCj5/Vue396jBhWx+8=User-Agent: Mandrill-Webhook/1.0Host: requestb.inContent-Type: application/x-www-form-urlencodedContent-Length: 4043Connection: closeAccept: */*mandrill_events=%5B%7B%22event%22%3A%22click%22%2C%22msg%22%3A%7B%22ts%22%3A1365109999%2C%22subject%22%3A%22This+an+example+webhook+message%22%2C%22email%22%3A%22example.webhook%40mandrillapp.com%22%2C%22sender%22%3A%22example.sender%40mandrillapp.com%22%2C%22tags%22%3A%5B%22webhook-example%22%5D%2C%22opens%22%3A%5B%7B%22ts%22%3A1365111111%7D%5D%2C%22clicks%22%3A%5B%7B%22ts%22%3A1365111111%2C%22url%22%3A%22http%3A%5C%2F%5C%2Fmandrill.com%22%7D%5D%2C%22state%22%3A%22sent%22%2C%22metadata%22%3A%7B%22user_id%22%3A111%7D%2C%22_id%22%3A%22exampleaaaaaaaaaaaaaaaaaaaaaaaaa%22%2C%22_version%22%3A%22exampleaaaaaaaaaaaaaaa%22%7D%2C%22ip%22%3A%22127.0.0.1%22%2C%22location%22%3A%7B%22country_short%22%3A%22US%22%2C%22country%22%3A%22United+States%22%2C%22region%22%3A%22Oklahoma%22%2C%22city%22%3A%22Oklahoma+City%22%2C%22latitude%22%3A35.4675598145%2C%22longitude%22%3A-97.5164337158%2C%22postal_code%22%3A%2273101%22%2C%22timezone%22%3A%22-05%3A00%22%7D%2C%22user_agent%22%3A%22Mozilla%5C%2F5.0+%28Macintosh%3B+U%3B+Intel+Mac+OS+X+10.6%3B+en-US%3B+rv%3A1.9.1.8%29+Gecko%5C%2F20100317+Postbox%5C%2F1.1.3%22%2C%22user_agent_parsed%22%3A%7B%22type%22%3A%22Email+Client%22%2C%22ua_family%22%3A%22Postbox%22%2C%22ua_name%22%3A%22Postbox+1.1.3%22%2C%22ua_version%22%3A%221.1.3%22%2C%22ua_url%22%3A%22http%3A%5C%2F%5C%2Fwww.postbox-inc.com%5C%2F%22%2C%22ua_company%22%3A%22Postbox%2C+Inc.%22%2C%22ua_company_url%22%3A%22http%3A%5C%2F%5C%2Fwww.postbox-inc.com%5C%2F%22%2C%22ua_icon%22%3A%22http%3A%5C%2F%5C%2Fcdn.mandrill.com%5C%2Fimg%5C%2Femail-client-icons%5C%2Fpostbox.png%22%2C%22os_family%22%3A%22OS+X%22%2C%22os_name%22%3A%22OS+X+10.6+Snow+Leopard%22%2C%22os_url%22%3A%22http%3A%5C%2F%5C%2Fwww.apple.com%5C%2Fosx%5C%2F%22%2C%22os_company%22%3A%22Apple+Computer%2C+Inc.%22%2C%22os_company_url%22%3A%22http%3A%5C%2F%5C%2Fwww.apple.com%5C%2F%22%2C%22os_icon%22%3A%22http%3A%5C%2F%5C%2Fcdn.mandrill.com%5C%2Fimg%5C%2Femail-client-icons%5C%2Fmacosx.png%22%2C%22mobile%22%3Afalse%7D%2C%22url%22%3A%22http%3A%5C%2F%5C%2Fmandrill.com%22%7D%2C%7B%22event%22%3A%22click%22%2C%22msg%22%3A%7B%22ts%22%3A1365109999%2C%22subject%22%3A%22This+an+example+webhook+message%22%2C%22email%22%3A%22example.webhook%40mandrillapp.com%22%2C%22sender%22%3A%22example.sender%40mandrillapp.com%22%2C%22tags%22%3A%5B%22webhook-example%22%5D%2C%22opens%22%3A%5B%7B%22ts%22%3A1365111111%7D%5D%2C%22clicks%22%3A%5B%7B%22ts%22%3A1365111111%2C%22url%22%3A%22http%3A%5C%2F%5C%2Fmandrill.com%22%7D%5D%2C%22state%22%3A%22sent%22%2C%22metadata%22%3A%7B%22user_id%22%3A111%7D%2C%22_id%22%3A%22exampleaaaaaaaaaaaaaaaaaaaaaaaaa%22%2C%22_version%22%3A%22exampleaaaaaaaaaaaaaaa%22%7D%2C%22ip%22%3A%22127.0.0.1%22%2C%22location%22%3A%7B%22country_short%22%3A%22US%22%2C%22country%22%3A%22United+States%22%2C%22region%22%3A%22Oklahoma%22%2C%22city%22%3A%22Oklahoma+City%22%2C%22latitude%22%3A35.4675598145%2C%22longitude%22%3A-97.5164337158%2C%22postal_code%22%3A%2273101%22%2C%22timezone%22%3A%22-05%3A00%22%7D%2C%22user_agent%22%3A%22Mozilla%5C%2F5.0+%28Macintosh%3B+U%3B+Intel+Mac+OS+X+10.6%3B+en-US%3B+rv%3A1.9.1.8%29+Gecko%5C%2F20100317+Postbox%5C%2F1.1.3%22%2C%22user_agent_parsed%22%3A%7B%22type%22%3A%22Email+Client%22%2C%22ua_family%22%3A%22Postbox%22%2C%22ua_name%22%3A%22Postbox+1.1.3%22%2C%22ua_version%22%3A%221.1.3%22%2C%22ua_url%22%3A%22http%3A%5C%2F%5C%2Fwww.postbox-inc.com%5C%2F%22%2C%22ua_company%22%3A%22Postbox%2C+Inc.%22%2C%22ua_company_url%22%3A%22http%3A%5C%2F%5C%2Fwww.postbox-inc.com%5C%2F%22%2C%22ua_icon%22%3A%22http%3A%5C%2F%5C%2Fcdn.mandrill.com%5C%2Fimg%5C%2Femail-client-icons%5C%2Fpostbox.png%22%2C%22os_family%22%3A%22OS+X%22%2C%22os_name%22%3A%22OS+X+10.6+Snow+Leopard%22%2C%22os_url%22%3A%22http%3A%5C%2F%5C%2Fwww.apple.com%5C%2Fosx%5C%2F%22%2C%22os_company%22%3A%22Apple+Computer%2C+Inc.%22%2C%22os_company_url%22%3A%22http%3A%5C%2F%5C%2Fwww.apple.com%5C%2F%22%2C%22os_icon%22%3A%22http%3A%5C%2F%5C%2Fcdn.mandrill.com%5C%2Fimg%5C%2Femail-client-icons%5C%2Fmacosx.png%22%2C%22mobile%22%3Afalse%7D%2C%22url%22%3A%22http%3A%5C%2F%5C%2Fmandrill.com%22%7D%5D I want to decode it into a array, like this site: http://requestb.in/n1rhtpn1?inspect Thanks! Quote Link to comment Share on other sites More sharing options...
requinix Posted July 25, 2013 Share Posted July 25, 2013 $_POST Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 25, 2013 Share Posted July 25, 2013 Depends on how you're getting the post, but assuming you get the post data in the string $data: urldecode($data); Then, that is json encoded and should be usable in javascript as is. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 25, 2013 Share Posted July 25, 2013 $_POST Or yes, depending upon how you're getting it. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 25, 2013 Share Posted July 25, 2013 Depends on how you're getting the post, but assuming you get the post data in the string $data: urldecode($data); Then, that is json encoded and should be usable in javascript as is. And, from looking at your data, probably stripslashes() after. Quote Link to comment Share on other sites More sharing options...
cesarbmrg Posted July 25, 2013 Author Share Posted July 25, 2013 (edited) I did that but wont work! Why? <?php $data = $_POST["mandrill_events"]; $unescaped_data = stripslashes($data); $obj = json_decode($unescaped_data); $event=$obj->event; $city=$obj->location->city; $country=$obj->location->country; //date_default_timezone_set('America/Sao_Paulo'); //$today = date("Y-m-d H:i:s"); //require './class.phpmailer.php'; // Assemble the body of the email... $message_body = 'Prezado Balantel, Esse e o codigo do webhook!/n' .$event.'/n' .$city.'/n' .$country.'/n'. print_r($obj); mail('email', 'Balantel Webbook Open', $message_body); ?> Edited July 25, 2013 by cesarbmrg Quote Link to comment Share on other sites More sharing options...
requinix Posted July 25, 2013 Share Posted July 25, 2013 1. You shouldn't stripslashes() as those are there because of JSON and json_decode() will do it for you. 2. $obj is an array with multiple events. You need a loop. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 25, 2013 Share Posted July 25, 2013 OK, you didn't get your original data from $_POST, so what does this show: $_POST["mandrill_events"]; Also, use this at the top: error_reporting(E_ALL); ini_set('display_errors', '1'); Quote Link to comment Share on other sites More sharing options...
cesarbmrg Posted July 26, 2013 Author Share Posted July 26, 2013 <?php error_reporting(E_ALL); ini_set('display_errors', '1'); $data = $_POST["mandrill_events"]; //$a = print_r($data); //$unescaped_data = stripslashes($data); $obj = json_decode($data); $event=$obj->event; $city=$obj->location->city; $country=$obj->location->country; //date_default_timezone_set('America/Sao_Paulo'); //$today = date("Y-m-d H:i:s"); //require './class.phpmailer.php'; // Assemble the body of the email... $message_body = $event.' ' .$city.' ' .$country.' '. $data; mail('email', 'Balantel Webbook Open', $message_body); ?> this code returns only this at email: [{\"event\":\"open\",\"ts\":1374797119,\"user_agent\":\"Mozilla\\/5.0 (Windows NT 5.1) AppleWebKit\\/535.6 (KHTML, like Gecko) Chrome\\/16.0.897.0 Safari\\/535.6\",\"user_agent_parsed\":{\"type\":\"Browser\",\"ua_family\":\"Chrome\",\"ua_name\":\"Chrome 16.0.897.0\",\"ua_version\":\"16.0.897.0\",\"ua_url\":\"http:\\/\\/www.google.com\\/chrome\",\"ua_company\":\"Google Inc.\",\"ua_company_url\":\"http:\\/\\/www.google.com\\/\",\"ua_icon\":\"http:\\/\\/cdn.mandrill.com\\/img\\/email-client-icons\\/chrome.png\",\"os_family\":\"Windows\",\"os_name\":\"Windows XP\",\"os_url\":\"http:\\/\\/en.wikipedia.org\\/wiki\\/Windows_XP\",\"os_company\":\"Microsoft Corporation.\",\"os_company_url\":\"http:\\/\\/www.microsoft.com\\/\",\"os_icon\":\"http:\\/\\/cdn.mandrill.com\\/img\\/email-client-icons\\/windowsxp.png\",\"mobile\":false},\"ip\":\"69.194.234.35\",\"location\":{\"country_short\":\"US\",\"country\":\"United States\",\"region\":\"Texas\",\"city\":\"Houston\",\"latitude\":29.7632808685,\"longitude\":-95.3632736206,\"postal_code\":\"77069\",\"timezone\":\"-05:00\"},\"msg\":{\"ts\":1374796960,\"subject\":\"[JCI] Cadastro Realizado!\",\"email\":\"paulojoao406+alceu@gmail.com\",\"tags\":[],\"opens\":[{\"ts\":1374797119,\"ip\":\"69.194.234.35\",\"location\":\"Texas, US\",\"ua\":\"Windows\\/Windows XP\\/Chrome\\/Chrome 16.0.897.0\"}],\"clicks\":[],\"state\":\"sent\",\"_id\":\"8b2fa5f2dfb04e9ba58a05484638881f\",\"_version\":\"yYAgZ4zgJHoLpoQ1_zsnJQ\",\"sender\":\"marketing@jcicomercio.com.br\",\"template\":null}}] Quote Link to comment Share on other sites More sharing options...
requinix Posted July 26, 2013 Share Posted July 26, 2013 2. $obj is an array with multiple events. You need a loop. Quote Link to comment Share on other sites More sharing options...
cesarbmrg Posted July 26, 2013 Author Share Posted July 26, 2013 This loop is not standard because some vars are deeper, i dont know how to do it. =( Im not an expert lol but first i must remove the slashes and the stripslashes did not work Quote Link to comment Share on other sites More sharing options...
requinix Posted July 26, 2013 Share Posted July 26, 2013 It. Is. An. Array. There. May. Be. More. Than. One. Item. In. That. Array. You. Need. A. Loop. Quote Link to comment Share on other sites More sharing options...
cesarbmrg Posted July 26, 2013 Author Share Posted July 26, 2013 I. Know. What. An. Array. Means. I. Dont. Know. How. To. Loop. It. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 26, 2013 Share Posted July 26, 2013 Oh, I didn't know "this loop is not standard" meant "I understand that a loop is the standard solution". foreach (best option) for while do/while Quote Link to comment 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.