Search code examples
phpjsonurlencodewebhooksfreshdesk

PHP Webhook Receiver not receiving data from Freshdesk


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      = '[email protected]';
$subject = $freshsubject;
$message = $freshticket;
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\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('[email protected]',
     '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:"[email protected]";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 <[email protected]>";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}}";}}

Solution

  • $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'];