Mandrill:验证PHP中的webhook

时间:2015-10-14 14:34:27

标签: php arrays json authentication mandrill

我需要使用PHP验证mandrill webhook。我已经按照here提到的所有步骤进行了操作,但是当我传递$ _POST数据时,它会抛出错误,说我无法将数组附加到字符串中。我理解错误,但我无法找到解释如何解决这个问题的任何内容。这就是我现在所拥有的:

Mandrill提供的签名生成器。

/**
 * Generates a base64-encoded signature for a Mandrill webhook request.
 * @param string $webhook_key the webhook's authentication key
 * @param string $url the webhook url
 * @param array $params the request's POST parameters
 */
protected 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));
}

我的json测试数据通过POST方法传递。

$jsonPost = ["mandrill_events" => '{
            "0": {
                "event": "hard_bounce",
                "msg": {
                    "ts": 1365109999,
                    "subject": "Thisanexamplewebhookmessage",
                    "email": "example@email.com",
                    "sender": "example.sender@mandrillapp.com",
                    "tags": [
                        "webhook-example"
                    ],
                    "opens": [],
                    "clicks": [],
                    "state": "sent",
                    "metadata": {
                        "user_id": 111
                    },
                    "_id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
                    "_version": "exampleaaaaaaaaaaaaaaa"
                },
                "_id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
                "ts": 1384954004
            }
        }'];

我在以下结构中收到此json数据。

Array ( [mandrill_events] => Array ( [1] => Array ( [event] => hard_bounce [msg] => Array ( [ts] => 1365109999 [subject] => Thisanexamplewebhookmessage [email] => example@email.com [sender] => example.sender@mandrillapp.com [tags] => Array ( [0] => webhook-example ) [opens] => Array ( ) [clicks] => Array ( ) [state] => sent [metadata] => Array ( [user_id] => 111 ) [_id] => exampleaaaaaaaaaaaaaaaaaaaaaaaaa [_version] => exampleaaaaaaaaaaaaaaa ) [_id] => exampleaaaaaaaaaaaaaaaaaaaaaaaaa [ts] => 1384954004 ) ) [responseType] => HTML ) 

我如何调用generateSignature。

$signature = $this->generateSignature($webhook_key, $url, $_POST);

错误。

Error 8 in line 40 of ... . Array to string conversion 5

第40行是:

$signed_data .= $value;

可能是什么问题?我实施了什么错误吗?

0 个答案:

没有答案