[英]PHP JSON decode Amazon SNS
I'm having issues decoding certain Amazon SNS notifications using PHP's json_decode
(5.3.10-1ubuntu3.5) and have not yet been able to figure out why despite the json being marked as valid using jsonlint.com.我在使用 PHP 的
json_decode
(5.3.10-1ubuntu3.5) 解码某些 Amazon SNS 通知时遇到问题,尽管使用 jsonlint.com 将 json 标记为有效,但我仍然无法弄清楚原因。 Can someone point me in the correct direction?有人可以指出我正确的方向吗? Here are two examples:
这里有两个例子:
{
"Type" : "Notification",
"MessageId" : "8q787fm3-f7fe-5sf4-863e-331bre627f24",
"TopicArn" : "arn:aws:sns:us-east-1:1234567890:ses-bounces-topic",
"Message" : "{\"notificationType\":\"Bounce\",\"bounce\":{\"reportingMTA\":\"dns; a193-136.smtp-out.amazonses.com\",\"bounceType\":\"Transient\",\"bouncedRecipients\":[{\"emailAddress\":\"email@example.com\",\"status\":\"5.0.0\",\"diagnosticCode\":\"smtp; 5.3.0 - Other mail system problem 571-'5.7.1 Message contains spam or virus or sender is blocked : 17624:1603463706|734463C' (delivery attempts: 0)\",\"action\":\"failed\"}],\"bounceSubType\":\"General\",\"timestamp\":\"2013-03-11T22:15:21.000Z\",\"feedbackId\":\"0000013d5b854265-18f16a12-8a99-11e2-aa8d-81a75f1af476-000000\"},\"mail\":{\"timestamp\":\"2013-03-11T22:14:51.000Z\",\"source\":\"otheremail@example.com\",\"messageId\":\"0000013d5b853e2a-820173e1-095e-4h91-9c91-03876f970534-000000\",\"destination\":[\"email@example.com\"]}}",
"Timestamp" : "2013-03-11T22:14:52.935Z",
"SignatureVersion" : "1",
"Signature" : "bY5gjFMgrVnK+4Qw867qHR0cLDXlgZmYb6EdiDAd4hNHMDab4J5MdldldEQwkSFslkdkDsdowlsKAdQvZ9HZwSmEcTRpwgg3Fpp5R/efVnTdUVfJkmBcnhijhWHpxSdEqN9m5vgPhg=",
"SigningCertURL" : "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-f3ecfb7224c72n3fe7bp5KDMMX6de32f.pem",
"UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:1234567890:ses-bounces-topic:73m9983aa-0f4b-4r87-a5d7-d43pb99c91af"
}
And和
{
"Type" : "Notification",
"MessageId" : "3c91t096-h331-5dm1-9u22-8822c3cdb7e8",
"TopicArn" : "arn:aws:sns:us-east-1:1234567890:ses-bounces-topic",
"Message" : "{\"notificationType\":\"Bounce\",\"bounce\":{\"reportingMTA\":\"dsn; aws-ses-mta-svc-iad-1d-i-ccb81arf.us-east-1.amazon.com\",\"bounceType\":\"Permanent\",\"bouncedRecipients\":[{\"emailAddress\":\"email@example.com\",\"status\":\"5.1.1\",\"diagnosticCode\":\"smtp; 550-5.1.1 The email account that you tried to reach does not exist. Please try\\\\n550-5.1.1 double-checking the recipient's email address for typos or\\\\n550-5.1.1 unnecessary spaces. Learn more at\\\\n550 5.1.1 http://support.google.com/mail/bin/answer.py?answer=6596 ht9si5931660qab.18 - gsmtp\",\"action\":\"failed\"}],\"bounceSubType\":\"General\",\"timestamp\":\"2013-03-08T16:15:40.000Z\",\"feedbackId\":\"0000015m4ac15133-4b74a3sf-890b-11e2-bf3f-53yadf2149d9-000000\"},\"mail\":{\"timestamp\":\"2013-03-08T16:15:39.000Z\",\"source\":\"otheremail@example.com\",\"messageId\":\"0000017d44c94bnf-a333d68b-8bed-4a2b-bdbf-4156zb5cdd9f-000000\",\"destination\":[\"example@sample.com\"]}}",
"Timestamp" : "2013-03-08T16:15:40.472Z",
"SignatureVersion" : "1",
"Signature" : "pYxerRQVHo0kgbLh4a/nri8Rveqdlb/CbPuXEkdCaBt7ulJ5G5gU6TYaUM94iFnCTBC9+5dLZRvydIsemFCiUQUArsh30tcgzBbb2rb7cuZoi09T4bYByN9FY=",
"SigningCertURL" : "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-f3mcfb7224c7235fe7bb5f79f96dd52p.pem",
"UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:1234567890:ses-bounces-topic:7p37c3za-0h4b-4j87-a5i7-d42cb90c9maf"
}
I see that jsonlint.com says it's valid JSON I just can't figure out why when I pass the contents of the RAW post to json_decode
it's returning null.我看到 jsonlint.com 说它是有效的 JSON 我只是不明白为什么当我将 RAW 帖子的内容传递给
json_decode
它返回 Z37A6259CC0C1DAE299A7D86648 时。 For the first level of JSON I'm using:对于 JSON 的第一级,我正在使用:
$result = json_decode($HTTP_RAW_POST_DATA);
if (!empty($result)) { }
I should add per E_ERROR's question I definitely have json_decode
as some/most of the notifications are parsed correctly.我应该根据 E_ERROR 的问题添加我肯定有
json_decode
,因为某些/大部分通知已正确解析。 I'm just getting a subset that for some reason aren't.我只是得到一个由于某种原因没有的子集。
UPDATE: It's been a while so my head is not in this code anymore but to answer Hasan's question.更新:已经有一段时间了,所以我的头不再在这段代码中,而是回答哈桑的问题。 Here is the working code and my comment about it.
这是工作代码和我对此的评论。
// json_decode does not like single quotes in the response so strip them here
$notification = str_replace("'", '', $HTTP_RAW_POST_DATA);
$result = json_decode($notification);
Hope this helps!希望这可以帮助!
This is a really old question, but I had the same problem decoding JSON received from Amazon SNS.这是一个非常古老的问题,但我在解码从 Amazon SNS 收到的 JSON 时遇到了同样的问题。 json_decode () was not working and json_last_error () was returning JSON_ERROR_SYNTAX.
json_decode () 不工作并且 json_last_error () 返回 JSON_ERROR_SYNTAX。
After trying (and failing) with string replacement, I decided to disable "Include Original Headers" in Amazon SES (which sends the bounce notification to Amazon SNS).在尝试使用字符串替换(但失败)后,我决定在 Amazon SES 中禁用“包含原始标头”(它将退回通知发送到 Amazon SNS)。 Suddenly everything worked fine.
突然间一切正常。
For completeness, my PHP code to decode the string is as follows:为了完整起见,我解码字符串的 PHP 代码如下:
$decoded = json_decode (trim ($jsonFromAWS), true);
if (is_array ($decoded) && isset ($decoded['Message']) && !is_array ($decoded['Message']) && $decoded['Message'] !== '')
{
$decoded['Message'] = json_decode (trim ($decoded['Message']), true);
}
Hope that helps anyone else bumping into this question!希望能帮助其他遇到这个问题的人!
Check this link.检查此链接。
Receiving Amazon SNS Messages in PHP 在 PHP 中接收 Amazon SNS 消息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.