从SendGrid事件中抓取自定义参数Webhook PHP

时间:2017-04-03 22:01:46

标签: php sendgrid sendgrid-api-v3

我在通过SendGrid发送电子邮件时尝试提取自定义参数,并在通过事件通知保存时将其拉入我的数据库。

我有事件通知设置,以便在SendGrid处理电子邮件后,将其保存到我的数据库中,我无法弄清楚如何提取我发送的自定义参数。

我发送这样的论点:

$mail->addCustomArg("campaign", "welcome69");

然后遇到试图接收它的问题(在SendGrid发布之后)

$data = file_get_contents("php://input");
$events = json_decode($data, true);

foreach ($events as $event) {
$sg_event_id = $event['sg_event_id'];
$unique_args = $event['unique_args'];
}

在底部代码snipet中,变量$ unique_args有效,第二个没有。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

Sendgrid POSTs to your hook is a JSON Object。您可以通过编写类似

的内容直接检索unique_args
$unique_args = $events['unique_args']; // this is an associative array.

您可以使用foreach循环以这种方式遍历关联数组:

foreach($unique_args as $field => $value) {
}