PHP解析Mailgun Webhook消息头

时间:2016-03-14 14:22:32

标签: php json webhooks

我在这里发现了许多关于Mailgun json响应的帖子,但没有人回答我的问题

Mailgun将此字符串作为webhook POST请求的一部分返回:

[["Received", "by luna.mailgun.net with SMTP mgrt 8734663311733; Fri, 03 May 
2013 18:26:27 +0000"], ["Content-Type", ["multipart/alternative", {"boundary": 
"eb663d73ae0a4d6c9153cc0aec8b7520"}]], ["Mime-Version", "1.0"], ["Subject", 
"Test bounces webhook"], ["From", "Bob <bob@domain.com.cz>"], ["To", "Alice 
<alice@example.com>"], ["Message-Id", " 
<20130503182626.18666.16540@domain.com>"], ["List-Unsubscribe", "
<mailto:u+na6tmy3ege4tgnldmyytqojqmfsdembyme3tmy3cha4wcndbgaydqyrgoi6wszdpovr
hi5dinfzw63tfmv4gs43uomstimdhnvqws3bomnxw2jtuhusteqjgmq6tm@example.com>"], ["X- 
Mailgun-Sid", "WyIwNzI5MCIsICJhbGljZUBleGFtcGxlLmNvbSIsICI2Il0="], ["X-Mailgun- 
Variables", "{"my_var_1": "Mailgun Variable #1", "my-var-2": "awesome"}"], 
["Date", "Fri, 03 May 2013 18:26:27 +0000"], ["Sender", "bob@domain.com"]]

问题是,如何使用PHP解析它? json_decode返回null。我需要从该字符串中获取Subject。感谢

这是我得到的完整回复:

[
attachment-count => 1,
code => 550,
domain => "domain.com",
error => "5.1.1 The email account that you tried to reach does not exist. Please try5.1.1 double-checking the recipient's email address for typos or5.1.1 unnecessary spaces. Learn more at5.1.1 http://support.example.com/mail/bin/answer.py",
event => "bounced",
message-headers => "[["Received", "by luna.mailgun.net with SMTP mgrt 8734663311733; Fri, 03 May 2013 18:26:27 +0000"], ["Content-Type", ["multipart/alternative", {"boundary": "eb663d73ae0a4d6c9153cc0aec8b7520"}]], ["Mime-Version", "1.0"], ["Subject", "Test bounces webhook"], ["From", "Bob <bob@domain.com>"], ["To", "Alice <alice@example.com>"], ["Message-Id", "<20130503182626.18666.16540@domain.com>"], ["List-Unsubscribe", "<mailto:u+na6tmy3ege4tgnldmyytqojqmfsdembyme3tmy3cha4wcndbgaydqyrgoi6wszdpovrhi5dinfzw63tfmv4gs43uomstimdhnvqws3bomnxw2jtuhusteqjgmq6tm@lidskasila.cz>"], ["X-Mailgun-Sid", "WyIwNzI5MCIsICJhbGljZUBleGFtcGxlLmNvbSIsICI2Il0="], ["X-Mailgun-Variables", "{"my_var_1": "Mailgun Variable #1", "my-var-2": "awesome"}"], ["Date", "Fri, 03 May 2013 18:26:27 +0000"], ["Sender", "bob@domain.com.cz"]]",
Message-Id => "<20130503182626.18666.16540@lidskasila.cz>",
recipient => "alice@example.com",
signature => "0359cb85c5b22e8de04232f74a77b94d41dc539e0c64034f6787562648bf638c",
timestamp => 1457955019,
token => "cd5808bd17b3523cbbd18426841cec0e0c897d4c325d2c9621",
X-Mailgun-Sid => "WyIwNzI5MCIsICJhbGljZUBleGFtcGxlLmNvbSIsICI2Il0="
]

1 个答案:

答案 0 :(得分:1)

问题是message-headers变量具有正确的JSON格式。至于Mailgun - 保留的标题顺序。所以这是主题值的代码:

$tmp = $_POST['message-headers'];
$data = json_decode($tmp);
$subject = $data[3][1];