从 Mandrill Webhook 收到的无效 JSON 字符串

Invalid JSON String Received from Mandrill Webhook

JSON 从 Mandrill 收到的字符串:

[{"event":"hard_bounce","_id":"5760ab383b1b4e358f10cfe759440dce","msg":{"ts":1426810207,"_id":"5760ab383b1b4e358f10cfe759440dce", "state":"bounced","subject":"Cocktail Party","email":"XXXXXX@XXX.com.XX","tags":[],"smtp_events" :[],"resends":[],"_version":"Nes5DEa8tOq5Z6m_b9AWIA","diag":"smtp;501 5.1.3 邮箱地址语法错误"XXXXXX@XXX.com.XX"(不可打印字符)","bgtools_code":10,"sender":"info-noreply@inviter.com","template":null,"bounce_description":"bad_mailbox"},"ts":1426822720}]

更新:

这是从 mandrill 收到的另一个无效字符串

[{"event":"hard_bounce","_id":"cddb25d2023a486a89454d5c6cefc4c9","msg":{"ts":1427904051,"_id":"cddb25d2023a486a89454d5c6cefc4c9","state":"bounced","subject":"Aniversary Cocktail Party","email":"XXXXX@XXX.com","tags":[],"smtp_events":[],"resends":[],"_version":"CvawLhm-1KO4OY_FsZ3uSA","diag":"smtp;550 No Such User Here"", "bgtools_code":10,"sender":"info-noreply@inviter.com","template":null,"bounce_description":"bad_mailbox"},"ts":1427935354}]

预期 JSON 字符串:

[
    {
        "event": "hard_bounce",
        "_id": "5760ab383b1b4e358f10cfe759440dce",
        "msg": {
            "ts": 1426810207,
            "_id": "5760ab383b1b4e358f10cfe759440dce",
            "state": "bounced",
            "subject": "Cocktail Party",
            "email": "XXXXXX@XXX.com.XX",
            "tags": [],
            "smtp_events": [],
            "resends": [],
            "_version": "Nes5DEa8tOq5Z6m_b9AWIA",
            "diag": "smtp;501 5.1.3 Syntax error in mailbox address 'XXXXXX@XXX.com.XX' (non-printable character)",
            "bgtools_code": 10,
            "sender": "info-noreply@inviter.com",
            "template": null,
            "bounce_description": "bad_mailbox"
        },
        "ts": 1426822720
    }
]

验证JSON字符串PHP代码:

function validateMandrillJSONString($JSONString){
    $JSONString = json_decode($JSONString);
    if(is_array($JSONString)){
        if(empty($JSONString)){
            return FALSE;
        }
        else{
            return TRUE;
        }
    }
    else{
        return FALSE;
    }
}

问题: 当我将 Received JSON 字符串发送到上面的 PHP 函数时,它返回 FALSE。然后发现 JSON String With Double Quotes("diag":"smtp;501 5.1.3 Syntax error in mailbox address "XXXXXX@XXX.com.XX" (non-可打印字符)").

不仅是上面的字符串。我有几个类似的字符串。例如,当用户在主题中给出双引号时(嗨 "Madhu",你好吗?)我得到了同样的错误。

是山魈的问题还是我这边的问题。如果是我这边,我该如何解决?

提前致谢。

我可以说,这个 bug 是 mandrill 自己造成的。我从山魈那里得到了无效 JSON。键值内双引号的主要问题 "diag".

这是我使用字符串替换的临时修复。

//Data received from mandrill
$queueData = '[{"event":"hard_bounce","_id":"5760ab383b1b4e358f10cfe759440dce","msg":{"ts":1426810207,"_id":"5760ab383b1b4e358f10cfe759440dce","state":"bounced","subject":"Cocktail Party","email":"XXXXXX@XXX.com.XX","tags":[],"smtp_events":[],"resends":[],"_version":"Nes5DEa8tOq5Z6m_b9AWIA","diag":"smtp;501 5.1.3 Syntax error in mailbox address "XXXXXX@XXX.com.XX" (non-printable character)","bgtools_code":10,"sender":"info-noreply@inviter.com","template":null,"bounce_description":"bad_mailbox"},"ts":1426822720}]';

$queueData = str_replace(':""',':"""',$queueData); // Temp Fix
$queueData = str_replace('""','"',$queueData); // Temp Fix

欢迎提出任何建议

谢谢。