PHP中的AWS SNS HTTP订阅确认

Pra*_*esh 5 php amazon-web-services amazon-sns aws-php-sdk laravel-5.1

我无法获得PHP中AWS SNS Http连接的确认。我的应用程序是在Laravel 5.1上开发的

在AWS中,我创建了一个主题并添加了一个订阅。我已将端点选择为HTTP并提供了URL http://myurl.com/sns

我的PHP代码如下

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 );
    }
  }
Run Code Online (Sandbox Code Playgroud)

我的路线文件条目是:

Route::get('/sns', [
'as'   => 'sns',
'uses' => 'SnsEndpointController@getSnsMessage',
]);
Run Code Online (Sandbox Code Playgroud)

在浏览器中,当我调用URL – http://myurl.com/sns时,出现以下错误。

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
Run Code Online (Sandbox Code Playgroud)

我的作曲家有以下内容:

"aws/aws-sdk-php-laravel": "^3.1",
"aws/aws-php-sns-message-validator": "^1.2"
Run Code Online (Sandbox Code Playgroud)

关于如何解决此错误并获得我的订阅确认的任何帮助吗?

Use*_*456 4

转到“VerifyCsrfToken”文件。添加

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
    ];
}
Run Code Online (Sandbox Code Playgroud)

转到您的路线文件,将帖子路线添加到您的方法中

Route::post('sns', 'SnsEndpointController@getSnsMessage')->name('snsendpointcontroller.getsnsmessage');
Run Code Online (Sandbox Code Playgroud)

编辑你的方法

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

您将能够在错误日志或输出文件中看到订阅 URL