AWS SNS HTTP subscription confirmation in PHP
Asked Answered
L

2

6

I am unable to get the confirmation of AWS SNS Http connection in PHP. My application is developed on Laravel 5.1

In AWS I have created a Topic and added a subscription. I have selected endpoint as HTTP and provided the URL http://myurl.com/sns.

My PHP code is below

public function getSnsMessage()
{
   $message = Message::fromRawPostData();
   $validator = new MessageValidator();
   // Validate the message and log errors if invalid.
   try {
       $validator->validate($message);
    }catch (InvalidSnsMessageException $e) {
       // Pretend we're not here if the message is invalid.
       http_response_code(404);
        error_log('SNS Message Validation Error: ' . $e->getMessage());
       die();
    }

    // Check the type of the message and handle the subscription.
   if ($message['Type'] === 'SubscriptionConfirmation') {
       // Confirm the subscription by sending a GET request to the SubscribeURL
       file_get_contents(public_path() . '/awssns.txt','SUB URL MESSAGE = '.$message['SubscribeURL'].PHP_EOL, FILE_APPEND );
    }
  }

My route file entry is:

Route::get('/sns', [
'as'   => 'sns',
'uses' => 'SnsEndpointController@getSnsMessage',
]);

In the browser when I call the URL – http://myurl.com/sns, I get the below error.

RuntimeException in Message.php line 35:SNS message type header not provided.
1.    in Message.php line 35
2.    at Message::fromRawPostData() in SnsEndpointController.php line 26
3.    at SnsEndpointController->getSnsMessage(object(Request))
4.    at call_user_func_array(array(object(SnsEndpointController), 
       'getSnsMessage'), array(object(Request))) in Controller.php line 256

I have the following in my composer:

"aws/aws-sdk-php-laravel": "^3.1",
"aws/aws-php-sns-message-validator": "^1.2"

Any help on how to resolve this error and to get confirmation of my subscription?

Lima answered 18/7, 2017 at 3:3 Comment(0)
P
4

Go to "VerifyCsrfToken" file . Add

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [

        'sns' // your call back URL
    ];
}

Go to your routes file add a post route to your method

Route::post('sns', 'SnsEndpointController@getSnsMessage')->name('snsendpointcontroller.getsnsmessage');

Edit your method

public function getSnsMessage()
{
//All Of your code here
error_log($message['SubscribeURL']); // To check your are receiving URL
}

you will be able to see the the subscription URL in error log or in your out file

Prose answered 13/10, 2017 at 6:12 Comment(2)
thanks for your tip. After adding the except in VerifyCsrfToken... it started working. Thank you very much.Lima
@Lima Are you able to resolve this? I am trying it using a Plain PHP but unable to resolve it.Demarcusdemaria
M
0

sorry for the late answer ...!

make sure your route should be any or post and remove csrf protection for that route because sns send post data....

tell me if it's works..

Morven answered 10/9, 2017 at 3:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.