[英]How to Read Amazon SNS JSON Response
如何從Amazon SNS對http端點的響應中獲取TranscriptionJobName和TranscriptionJobStatus?
當我在下面嘗試我的代碼時,文本文件中的name變量為空,但是來自Amazon的消息記錄到我的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得到的結果是:
{
我的代碼能夠讀取發送到端點的消息,但是嘗試進一步解析為響應,將在我用來調試的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'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.