0

I'm not a PHP programmer but I have successfully gotten webhooks working with CaptainForms and Formstack before thanks to alot of research and reading. However, I'm having a hard time getting my webhook file to work in FreskDesk. I've attempted to use a few different php codebases but I'm not getting any data received. I know FreshkDesk is working properly because I used Request.in and see the data is being sent there after it is triggered in FreskDesk.

What am I missing or doing wrong?

I'm using POST and I've tried both content types. Content-Type: application/json Content-Type: application/x-www-form-urlencoded

Example 1

<?php

if($json = json_decode(file_get_contents("php://input"), true)) {
    print_r($json);
     $formdata = $json;
 } else {
     print_r($_POST);
     $formdata = $_POST;
 }
$formdata = ARRAY();
foreach ($_POST as $key => $value) { 
$value = stripslashes($value);
$formdata[$key]=$value;
}


$freshsubject = $formdata['freshdesk_webhook[ticket_subject]'];
$freshticket =  $formdata['freshdesk_webhook[ticket_id]'];

$to      = 'ME@example.com';
$subject = $freshsubject;
$message = $freshticket;
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?> 

Example 2

<?php

function stripslashes_deep($value) {
  $value = is_array($value) ?
    array_map('stripslashes_deep', $value) :
    stripslashes($value);
  return $value;
}                 
if (get_magic_quotes_gpc()) {
  $unescaped_post_data = stripslashes_deep($_POST);
} else {
  $unescaped_post_data = $_POST;
}
$form_data = json_decode($unescaped_post_data['data_json']);

$page_id = $_POST['freshdesk_webhook[ticket_id]'];
$page_url = $_POST['freshdesk_webhook[ticket_url]'];
$variant = $_POST['freshdesk_webhook[ticket_subject]'];


$message_body = <<<EOM
Email: $email_address \n
Page ID: $page_id \n
URL: $page_url \n
Variant: $variant \n
EOM;
mail('me@example.com',
     'New Service Request Submission!',
     $message_body);
?>

These are the field from Request.in when using x-www-form-urlencoded

freshdesk_webhook[ticket_subject]: Request for Bob : Purchase Test
freshdesk_webhook[ticket_id]: SR-49
freshdesk_webhook[ticket_url]: https://test.freshservice.com/helpdesk/tickets/49

This is the relevant json

{"freshdesk_webhook":{"ticket_id":"SR-51","ticket_subject":"Request for Bob : Purchase Test","ticket_description":"","ticket_url":"https://test.freshservice.com/helpdesk/tickets/51","triggered_event":"{approval_status:{from:0,to:1}}"}}

I did 2 of them to see if $formdata had any data
file_put_contents("data.txt", serialize($formdata));

a:1:{s:17:"freshdesk_webhook";N;}

This is the $_Post data
file_put_contents("post.txt", serialize($_POST));

a:1:{s:17:"freshdesk_webhook";a:35:{s:19:"approval_portal_url";s:0:"";s:12:"approval_url";s:0:"";s:13:"helpdesk_name";s:18:"Service Desk";s:18:"ticket_agent_email";s:0:"";s:17:"ticket_agent_name";s:0:"";s:23:"ticket_bgmga_onboarding";s:0:"";s:22:"ticket_department_name";s:0:"";s:18:"ticket_description";s:0:"";s:18:"ticket_due_by_time";s:39:"Sat, Mar 10, 2018 at  3:48 PM GMT -0500";s:17:"ticket_group_name";s:0:"";s:9:"ticket_id";s:5:"SR-74";s:13:"ticket_impact";s:3:"Low";s:17:"ticket_portal_url";s:49:"https://test.freshservice.com/support/tickets/74";s:15:"ticket_priority";s:6:"Medium";s:17:"ticket_public_url";s:110:"https://test.freshservice.com/public/tickets/1b8a6d806c08da6384a6f2f21ce0ead4dba9032bf2b344b168d2d5f6d83ee00b";s:24:"ticket_requester_address";s:0:"";s:22:"ticket_requester_email";s:15:"my@example.com";s:26:"ticket_requester_firstname";s:6:"Bob";s:25:"ticket_requester_lastname";s:4:"Thomas";s:21:"ticket_requester_name";s:11:"Bob Thomas";s:22:"ticket_requester_phone";s:0:"";s:26:"ticket_service_item_fields";s:792:"
<table style='margin:0px;padding:0px;'>
<tr>
<td style='text-align:left; vertical-align:top;'>
 Item Name 
</td><td style='vertical-align:top;'> : </td>
<td style='text-align:left; vertical-align:top;'>
 Purchase Product Test 
</td></tr><tr>
<td style='text-align:left; vertical-align:top;'>
 Category 
</td><td style='vertical-align:top;'> : </td>
<td style='text-align:left; vertical-align:top;'>
 Purchasing Test 
</td></tr><tr>
<td style='text-align:left; vertical-align:top;'>
 Cost 
</td><td style='vertical-align:top;'> : </td>
<td style='text-align:left; vertical-align:top;'>
 $ 100.00 
</td></tr><tr>
<td style='text-align:left; vertical-align:top;'>
 Quantity 
</td><td style='vertical-align:top;'> : </td>
<td style='text-align:left; vertical-align:top;'>
 1 
</td></tr></table>
";s:27:"ticket_service_request_cost";s:8:"$ 100.00";s:33:"ticket_service_request_item_names";s:21:"Purchase Product Test";s:28:"ticket_service_requested_for";s:29:"Bob Thomas <my@example.com>";s:13:"ticket_source";s:6:"Portal";s:13:"ticket_stages";s:0:"";s:13:"ticket_status";s:4:"Open";s:14:"ticket_subject";s:47:"Request for Bob Thomas : Purchase Product Test";s:11:"ticket_tags";s:0:"";s:11:"ticket_type";s:15:"Service Request";s:14:"ticket_urgency";s:3:"Low";s:10:"ticket_url";s:50:"https://test.freshservice.com/helpdesk/tickets/74";s:34:"ticket_visible_service_item_fields";s:437:"
<table style='margin:0px;padding:0px;'>
<tr>
<td style='text-align:left; vertical-align:top;'>
 Item Name 
</td><td style='vertical-align:top;'> : </td>
<td style='text-align:left; vertical-align:top;'>
 Purchase Product Test 
</td></tr><tr>
<td style='text-align:left; vertical-align:top;'>
 Category 
</td><td style='vertical-align:top;'> : </td>
<td style='text-align:left; vertical-align:top;'>
 Purchasing Test 
</td></tr></table>
";s:15:"triggered_event";s:31:"{approval_status:{from:0,to:1}}";}}
Mr. KT
  • 15
  • 4
  • Example 1: is print_r($_POST) return something? – toor Mar 09 '18 at 17:23
  • I'm not sure. How can I verify? Right now, I point FreshDesk to my webhook url and don't get to see any results. I just receive an email with none of the data in the fields. – Mr. KT Mar 09 '18 at 17:33
  • You may save $_POST array dump as text file using file_put_contents function. Please post content of this file. https://stackoverflow.com/questions/3487595/storing-post-values-in-an-array-to-save-them-to-text-file – toor Mar 09 '18 at 18:12
  • Thanks Toor. I updated the original question with the requested content. – Mr. KT Mar 09 '18 at 20:59
  • Your $_POST is multi-dimensional array. Please try this $freshsubject = $_POST['freshdesk_webhook']['ticket_subject']; $freshticket = $_POST['freshdesk_webhook']['ticket_id']; Another way is $formdata = ARRAY(); foreach ($_POST as $key => $value) { if ($key == 'freshdesk_webhook') { foreach ($value as $k => $v){ $formdata[$k]=$v; } } } $freshsubject = $formdata['ticket_subject']; – toor Mar 10 '18 at 08:30
  • Or this $freshsubject = $_POST[0]['freshdesk_webhook']['ticket_subject']; – toor Mar 10 '18 at 12:11
  • Thanks Toor!!! You solved my issue and now my variables are getting the values. I was missing the ['freshdesk_webhook']. I also tested the new $formdata Array and that was also successful. I'm sure that will come in handy down the line. I will mark your answer as accepted if you post it separately. – Mr. KT Mar 10 '18 at 14:20
  • You're welcome! Thank you for the interesting question. – toor Mar 10 '18 at 14:30

1 Answers1

0
$freshsubject = $_POST['freshdesk_webhook']['ticket_subject'];
$freshticket = $_POST['freshdesk_webhook']['ticket_id'];

Another way is

foreach ($_POST as $key => $value) {
    if ($key == 'freshdesk_webhook') { 
        foreach ($value as $k => $v) {
            $formdata[$k]=$v;
        }
    }
}
$freshsubject = $formdata['ticket_subject'];
toor
  • 101
  • 5
  • Toor, I have another interesting question if you are interested in the challenge. It's actually Part 2 to this original question. https://stackoverflow.com/q/49289813/7601122 – Mr. KT Mar 15 '18 at 00:41