如何阅读 Amazon SNS JSON 响应
How to Read Amazon SNS JSON Response
如何从 Amazon SNS 对 http 端点的响应中获取 TranscriptionJobName 和 TranscriptionJobStatus?
当我尝试下面的代码时,我的文本文件中的名称变量为空,但来自亚马逊的消息登录到我的 file.txt 中就好了。
我在 file.txt 中从 SNS 获得的内容(省略了一些值):
Array
(
[Type] => Notification
[MessageId] => MSG_ID
[TopicArn] => TOPIC_ARN
[Message] => {"version":"0","id":"msg_id","detail-type":"Transcribe Job State Change","source":"aws.transcribe","account":"account_number","time":"2019-03-07T18:19:08Z","region":"us-east-1","resources":[],"detail":{"TranscriptionJobName":"702edfc","TranscriptionJobStatus":"COMPLETED"}}
[Timestamp] => 2019-03-07T18:19:09.194Z
[SignatureVersion] => 1
[Signature] => sign==
[SigningCertURL] => sign_cert.pem
[UnsubscribeURL] => unsubscribe_url
)
我从 namer.txt 得到的:
{
我的代码能够读取发送到端点的消息,但试图进一步解析响应 returns 我用来调试的 txt 文件中的空白..
我的代码尝试:
//Endpoint.php
//Fetch the raw POST body containing the message
$postBody = file_get_contents('php://input');
// JSON decode the body to an array of message data
$message = json_decode($postBody, true);
if ($message) {
//just for debugging put entire response in file.txt
file_put_contents('file.txt', print_r($message, true));
//if its a Notification
if ($message['Type'] === 'Notification'){
//then get the name of the Transcription Job
$name = $message['Message']['detail'][0]['TranscriptionJobName'];
//put that into namer.txt
file_put_contents('namer.txt', $name,true);
}
}
出于某种原因,$message['Message']
似乎仍然是一个字符串而不是数组。
您需要对其进行解码才能访问其中的元素:
// Decode
$message['Message'] = json_decode($message['Message'], true);
// Now you can access it using:
$name = $message['Message']['detail']['TranscriptionJobName'];
如何从 Amazon SNS 对 http 端点的响应中获取 TranscriptionJobName 和 TranscriptionJobStatus?
当我尝试下面的代码时,我的文本文件中的名称变量为空,但来自亚马逊的消息登录到我的 file.txt 中就好了。
我在 file.txt 中从 SNS 获得的内容(省略了一些值):
Array
(
[Type] => Notification
[MessageId] => MSG_ID
[TopicArn] => TOPIC_ARN
[Message] => {"version":"0","id":"msg_id","detail-type":"Transcribe Job State Change","source":"aws.transcribe","account":"account_number","time":"2019-03-07T18:19:08Z","region":"us-east-1","resources":[],"detail":{"TranscriptionJobName":"702edfc","TranscriptionJobStatus":"COMPLETED"}}
[Timestamp] => 2019-03-07T18:19:09.194Z
[SignatureVersion] => 1
[Signature] => sign==
[SigningCertURL] => sign_cert.pem
[UnsubscribeURL] => unsubscribe_url
)
我从 namer.txt 得到的:
{
我的代码能够读取发送到端点的消息,但试图进一步解析响应 returns 我用来调试的 txt 文件中的空白..
我的代码尝试:
//Endpoint.php
//Fetch the raw POST body containing the message
$postBody = file_get_contents('php://input');
// JSON decode the body to an array of message data
$message = json_decode($postBody, true);
if ($message) {
//just for debugging put entire response in file.txt
file_put_contents('file.txt', print_r($message, true));
//if its a Notification
if ($message['Type'] === 'Notification'){
//then get the name of the Transcription Job
$name = $message['Message']['detail'][0]['TranscriptionJobName'];
//put that into namer.txt
file_put_contents('namer.txt', $name,true);
}
}
出于某种原因,$message['Message']
似乎仍然是一个字符串而不是数组。
您需要对其进行解码才能访问其中的元素:
// Decode
$message['Message'] = json_decode($message['Message'], true);
// Now you can access it using:
$name = $message['Message']['detail']['TranscriptionJobName'];