Search code examples
phpamazon-web-servicesamazon-ses

Unable to receive any Posted Data or Subscribe URL against AWS SNS Service


I am trying to utilize SNS Service to subscribe to a Topic using the http protol

Following is piece of php code I have place on my server:-

<?php

require 'vendor/autoload.php';
require 'Aws/Sns/Message.php';
require 'Aws/Sns/MessageValidator.php';
require 'GuzzleHttp/Client.php';

use Aws\Sns\Message;
use Aws\Sns\MessageValidator;
use GuzzleHttp\Client;


// Make sure the request is POST
  if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
      file_put_contents("notification.txt", "Error 405\n", FILE_APPEND);
      http_response_code(405);
      die;
  }

try {
    $message = Message::fromRawPostData();
    file_put_contents("notification.txt", "\r\n-------\r\n", FILE_APPEND);
    file_put_contents("notification.txt", $_REQUEST , FILE_APPEND);
    $validator = new MessageValidator();
    $validator->validate($message);
} catch (Exception $e) {
    // Pretend we're not here if the message is invalid.
    file_put_contents("notification.txt", 'SNS Message Validation Error: ' . $e->getMessage() . "\n".$e->getTraceAsString()."\n", FILE_APPEND);
    echo($e);
    http_response_code(404);
    die();
}

/*
if ($message->get('Type') === 'SubscriptionConfirmation') {
    // Send a request to the SubscribeURL to complete subscription
    (new Client)->get($message->get('SubscribeURL'))->send();
    file_put_contents("notification.txt", "Subscription\n", FILE_APPEND);
} elseif ($message->get('Type') === 'Notification') {
    ob_start();
    var_dump($message);
    $result = ob_get_clean();
    file_put_contents("notification.txt", $result, FILE_APPEND);
}
*/

When I tried to "Confirm Subscription" by invoking it from the AWS Portal I am receiving a call but there is no header or body having the subscription URL. Can anyone help me follow the right direction?


Solution

  • Thanks to @Damir Kasipovic Comment above. The change of code from

    file_put_contents("notification.txt", $_REQUEST , FILE_APPEND); with 
    

    to this

    file_put_contents("notification.txt", file_get_contents('php://input') , FILE_APPEND)
    

    help me extract the data from the call

    {
    "Type": "SubscriptionConfirmation",
    "MessageId": "cc0fb4d8-***********-542a9f3dbb33",
    "Token": "2336412f37fb687f5d51e6************4795716f01db42d3b1",
    "TopicArn": "arn:aws:sns:**********Downloaded",
    "Message": "You have chosen to subscribe to the topic arn:aws:sns:*****nTo confirm the subscription, visit the SubscribeURL included in this message.",
    "SubscribeURL": "https://sns.us-east-1.amazonaws.com/*************1",
    "Timestamp": "2019-11-25T12:55:40.335Z",
    "SignatureVersion": "1",
    "Signature": "L2uX+OF1f39np2******************h0lmw==",
    "SigningCertURL": "https://sns.us-east-1.amazonaws.com/*******************.pem"
    

    }