PHP 解析 Mailgun Webhook 消息头

标签 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 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b6964694b6f64666a626525686466256871" rel="noreferrer noopener nofollow">[email protected]</a>>"], ["To", "Alice 
<<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ffef3f6fcfadffae7fef2eff3fab1fcf0f2" rel="noreferrer noopener nofollow">[email protected]</a>>"], ["Message-Id", " 
<<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fccecccdcfccc9cccfcdc4cecacecad2cdc4cacacad2cdcac9c8ccbc9893919d9592d29f9391" rel="noreferrer noopener nofollow">[email protected]</a>>"], ["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", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9efcf1fcdefaf1f3fff7f0b0fdf1f3" rel="noreferrer noopener nofollow">[email protected]</a>"]]

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

这是我得到的完整回复:

[
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 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f2d202d0f2b20222e2621612c2022" rel="noreferrer noopener nofollow">[email protected]</a>>"], ["To", "Alice <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aacbc6c3c9cfeacfd2cbc7dac6cf84c9c5c7" rel="noreferrer noopener nofollow">[email protected]</a>>"], ["Message-Id", "<<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c0e0c0d0f0c090c0f0d040e0a0e0a120d040a0a0a120d0a09080c7c5853515d5552125f5351" rel="noreferrer noopener nofollow">[email protected]</a>>"], ["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", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ceaca1ac8eaaa1a3afa7a0e0ada1a3e0adb4" rel="noreferrer noopener nofollow">[email protected]</a>"]]",
Message-Id => "<<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f8cac8c9cbc8cdc8cbc9c0cacecaced6c9c0cececed6c9cecdccc8b894919c8b93998b919499d69b82" rel="noreferrer noopener nofollow">[email protected]</a>>",
recipient => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f1e13161c1a3f1a071e120f131a511c1012" rel="noreferrer noopener nofollow">[email protected]</a>",
signature => "0359cb85c5b22e8de04232f74a77b94d41dc539e0c64034f6787562648bf638c",
timestamp => 1457955019,
token => "cd5808bd17b3523cbbd18426841cec0e0c897d4c325d2c9621",
X-Mailgun-Sid => "WyIwNzI5MCIsICJhbGljZUBleGFtcGxlLmNvbSIsICI2Il0="
]

最佳答案

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

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

关于PHP 解析 Mailgun Webhook 消息头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35990077/

相关文章:

reactjs - 如何在 React 中使用 API webhook 来接收通知

php - Symfony Twig Extension 破坏了其他服务 - 模板是否在安全之前完成?

php - 如何寻址mysql表的每一行?

php - Laravel artisan路由列表数据库连接错误

java - 在 recyclerview 中访问 JSON 数组中的项目成员

javascript - HIghcharts 和动态 json

PHP 5.6 MySQLi 关闭对等证书 cn 验证

javascript - 动态设置json选择器

azure - 无法在 Azure 中使用 Webhook 传递属性注册事件网格订阅

github - Jenkins + GitHub 插件 webhook 不起作用