【问题标题】:AWS SNS HTTP subscription confirmation in PHPPHP 中的 AWS SNS HTTP 订阅确认
【发布时间】:2017-12-22 17:32:00
【问题描述】:

我无法在 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 );
    }
  }

我的路由文件入口是:

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

当我在浏览器中调用 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

我的作曲家中有以下内容:

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

有关如何解决此错误并确认我的订阅的任何帮助?

【问题讨论】:

    标签: php amazon-web-services laravel-5.1 amazon-sns aws-php-sdk


    【解决方案1】:

    转到“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
        ];
    }
    

    转到您的路线文件 为您的方法添加发布路线

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

    修改你的方法

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

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

    【讨论】:

    • 感谢您的提示。在 VerifyCsrfToken... 添加异常后,它开始工作。非常感谢。
    • @Prabesh 你能解决这个问题吗?我正在尝试使用普通 PHP 但无法解决它。
    【解决方案2】:

    抱歉回复晚了...!

    确保您的路由应该是 any 或 post 并删除该路由的 csrf 保护,因为 sns 发送 post 数据......

    告诉我它是否有效..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-30
      • 2017-04-20
      • 2023-03-10
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      相关资源
      最近更新 更多