使用 PHP 验证 Mandrill Webhook

Authenticating Mandrill Webhook with PHP

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

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提供的例子?任何帮助将不胜感激。

谢谢! 大卫

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