Question:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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:
1 2 3 4 5 |
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.
1 2 3 4 5 6 7 |
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:
1 2 3 |
"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?
Answer:
Go to “VerifyCsrfToken” file .
Add
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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
1 2 |
Route::post('sns', 'SnsEndpointController@getSnsMessage')->name('snsendpointcontroller.getsnsmessage'); |
Edit your method
1 2 3 4 5 6 |
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