PHP Webhook Receiver无法从Freshdesk

时间:2018-03-09 17:08:50

标签: php json urlencode webhooks freshdesk

我不是一名PHP程序员,但我已经成功获得了使用CaptainForms和Formstack的webhooks,这要归功于大量的研究和阅读。但是,我很难让我的webhook文件在FreskDesk中运行。我试图使用一些不同的php代码库,但我没有收到任何数据。我知道FreshkDesk工作正常,因为我使用了Request.in,看到数据在FreskDesk中被触发后被发送到那里。

我错过了什么或做错了什么?

我正在使用POST,并且我尝试了两种内容类型。 Content-Type:application / json 内容类型:application / x-www-form-urlencoded

示例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);
?> 

示例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);
?>

使用x-www-form-urlencoded

时,这些是来自Request.in的字段
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

这是相关的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}}";}}

1 个答案:

答案 0 :(得分:0)

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

另一种方式是

foreach ($_POST as $key => $value) {
    if ($key == 'freshdesk_webhook') { 
        foreach ($value as $k => $v) {
            $formdata[$k]=$v;
        }
    }
}
$freshsubject = $formdata['ticket_subject'];