使用PHP验证Mandrill Webhook

时间:2016-01-15 15:30:42

标签: php mandrill webhooks

我使用Mandrill通过PHP发送电子邮件。我已经注册了一个webhook,所以我可以在我的应用程序和Mandrill中处理任何硬弹跳。我遇到的困难是生成X-Mandrill-Signature标头来验证请求。

Mandrill有关于如何执行此操作的文档here,但我未能做到正确。

这是从Mandrill发送给我的POST参数:

mandrill_events: [{"type":"whitelist","action":"add","entry":{"email":"example.webhook@mandrillapp.com","detail":"example details","created_at":"2014-01-15 12:03:19"},"ts":1452869880},{"type":"whitelist","action":"remove","entry":{"email":"example.webhook@mandrillapp.com","detail":"example details","created_at":"2014-01-15 12:03:19"},"ts":1452869880}]

我使用json_decode(stripslashes($_POST['mandrill_events']), true)将参数解码为数组,然后将数组传递给函数,如帮助文章所述:

function generateSignature($webhook_key, $url, $params) {
    $signed_data = $url;
    ksort($params);
    foreach ($params as $key => $value) {
        $signed_data .= $key;
        $signed_data .= $value;
    }

    return base64_encode(hash_hmac('sha1', $signed_data, $webhook_key, true));
}

使用这个我最终

"http://requestb.in/ylyl00yl0Array1Array"

代表$signed_data。我尝试了很多递归迭代数组键和值的变体,但无济于事。

有没有人成功使用过Mandrill提供的示例?任何帮助将不胜感激。

谢谢! 大卫

1 个答案:

答案 0 :(得分:3)

您应该验证Mandrill发送的POST参数,而不是事件JSON,即generateSignature($key, $url, $_POST)

相关问题