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! Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/ Share on other sites More sharing options...
requinix Posted July 25, 2013 Share Posted July 25, 2013 $_POST Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442165 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. Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442168 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. Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442169 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. Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442170 Share on other sites More sharing options...
cesarbmrg Posted July 25, 2013 Author Share Posted July 25, 2013 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); ?> Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442176 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. Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442178 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'); Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442181 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\":\"[email protected]\",\"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\":\"[email protected]\",\"template\":null}}] Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442210 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. Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442214 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 Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442223 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. Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442228 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. Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442312 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 Link to comment https://forums.phpfreaks.com/topic/280509-decode-webhook/#findComment-1442323 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.