Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS SNS HTTP subscription confirmation in PHP

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?

like image 261
Prabesh Avatar asked Jul 18 '17 03:07

Prabesh


Video Answer


1 Answers

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

like image 84
User123456 Avatar answered Oct 12 '22 23:10

User123456