PHP中的AWS SNS HTTP订阅确认
我无法在PHP中获得AWS SNS Http连接的确认。我的应用程序是在Laravel 5.1上开发的 在AWS中,我创建了一个主题并添加了订阅。我已选择端点作为HTTP并提供了URL 下面是我的PHP代码PHP中的AWS SNS HTTP订阅确认,php,amazon-web-services,laravel-5.1,amazon-sns,aws-php-sdk,Php,Amazon Web Services,Laravel 5.1,Amazon Sns,Aws Php Sdk,我无法在PHP中获得AWS SNS Http连接的确认。我的应用程序是在Laravel 5.1上开发的 在AWS中,我创建了一个主题并添加了订阅。我已选择端点作为HTTP并提供了URL 下面是我的PHP代码 public function getSnsMessage() { $message = Message::fromRawPostData(); $validator = new MessageValidator(); // Validate the message and
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–时,我得到以下错误
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"
有关如何解决此错误并获得订阅确认的任何帮助 很抱歉回答晚了 确保您的路由应该是any或post,并删除该路由的csrf保护,因为sns发送post数据 告诉我它是否有效。转到“VerifyCsrfToken”文件。 加 转到您的路由文件 向您的方法中添加post路由
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
}
编辑您的方法
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
}
您将能够在错误日志或out文件中看到订阅URL谢谢您的提示。在VerifyCsrfToken中添加except后。。。它开始工作了。非常感谢。@Prabesh你能解决这个问题吗?我正在尝试使用普通的PHP,但无法解决它。