Как читать ответ Amazon SNS JSON - PullRequest
0 голосов
/ 07 марта 2019

Как мне получить TranscriptionJobName и TranscriptionJobStatus из ответа Amazon SNS на конечную точку http?

Когда я пробую свой код ниже, переменная name в моем текстовом файле пуста, но сообщение от Amazon регистрируется в моем file.txt просто отлично.

Что я получаю от SNS в файле file.txt (некоторые значения опущены):

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:

{

Мой код может прочитать сообщение, отправленное конечной точке, но попытка дальнейшего анализа в ответ возвращает пустое значение в текстовом файле, который я использую для отладки.

Моя попытка кода:

    //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);






                                }

    }

1 Ответ

0 голосов
/ 08 марта 2019

По какой-то причине $message['Message'] по-прежнему является строкой, а не массивом.

Вам необходимо декодировать его, прежде чем вы сможете получить доступ к элементам в нем:

// Decode
$message['Message'] = json_decode($message['Message'], true);

// Now you can access it using:
$name = $message['Message']['detail']['TranscriptionJobName']; 
...