Skip to content

Instantly share code, notes, and snippets.

@lynsei
Last active January 1, 2022 13:14
Show Gist options
  • Save lynsei/e1491aee81b974ae9a80 to your computer and use it in GitHub Desktop.
Save lynsei/e1491aee81b974ae9a80 to your computer and use it in GitHub Desktop.
amazon sdk php :: receive aws SNS notifications (example)
<?php
/*+--------------------[objectcode: 12/20/2015 1:32:39 PM] [/end.automation]+--------------------+*/
/*
_____ ______ _________________________________ _____ ______ _______ (R)
| ||_____] | |______| | | | || \|______
|_____||_____]__| |______|_____ | |_____ |_____||_____/|______
`*+**: Disruptiveware, LLP. Authorized Sales USA/Canada
Creator: Lynsei Asynynivynya
Author: <[email protected]>
Workspace: TinyRuntime.com
Package: <deprecated code from ancient past>
GPG: http://disruptiveware.com/gpg.php
On: 11. December 2015 */
/****
* @desc: Receive Vendor SNS notifications from Amazon
* @vers: 0.1
*
**``~x&*/
/* rexNote->start *``~x&*``^
*
Receiving SNS Messages and Verifying Their Signature
Using the SNS Message Validator’s Message class, you can easily parse raw POST data from SNS:
*
* -CHO
** rexNote->end *``^~x&*``^ */
/*
require '../vendor/autoload.php';
$message = \Aws\Sns\Message::fromRawPostData();
echo $message->get('Message');
*/
/* rexNote->start *``~x&*``^
*
Amazon SNS sends different types of messages, including SubscriptionConfirmation, Notification, and UnsubscribeConfirmation. The formats of these messages are described in the Appendix: Message and JSON Formats section of the Amazon SNS Developer Guide.
Messages from Amazon SNS are signed. As a best practice, you should use the MessageValidator class to verify the signature and ensure a message was sent from Amazon SNS.
*
* -CHO
** rexNote->end *``^~x&*``^ */
/*
use Aws\Sns\Message;
use Aws\Sns\MessageValidator;
$message = Message::fromRawPostData();
// Validate the message
$validator = new MessageValidator();
$validator->validate($message);
*/
/* rexNote->start *``~x&*``^
*
Instances of \Aws\Sns\MessageValidator have two methods for validating messages, both of which take an instance of \Aws\Sns\Message as their only argument. validate (shown above) will throw an \Aws\Sns\Exception\InvalidSnsMessageException. isValid will return a Boolean — true for valid messages and false for invalid ones.
Confirming a Subscription to a Topic
In order for an HTTP(S) endpoint to receive messages, it must first be subscribed to an SNS topic. Subscriptions are confirmed over HTTP(S), so you’ll need to create and deploy an endpoint before you set up a subscription. SubscriptionConfirmation messages provide a URL you can use to confirm the subscription.
*
* -CHO
** rexNote->end *``^~x&*``^ */
/*
$message = \Aws\Sns\Message::fromRawPostData();
// Validate the message
$validator = new MessageValidator();
if ($validator->isValid($message)) {
file_get_contents($message->get('SubscribeURL'));
}
*/
/* rexNote->start *``~x&*``^
*
Handling Notifications
Let’s put it all together and add some extra code for handling both notifications and subscription control messages.
*
* -CHO
** rexNote->end *``^~x&*``^ */
/*[OC> 0 */
/*[OC> 1 */
/*[OC> 2 */ if ('POST' !== $_SERVER['REQUEST_METHOD']) {
/*[OC> 3 */ http_response_code(405);
/*[OC> 4 */ die;
/*[OC> 5 */ }
/*[OC> 6 */
/*[OC> 7 */ require 'path/to/vendor/autoload.php';
/*[OC> 8 */
/*[OC> 9 */ try {
/*[OC> 10 */ $message = \Aws\Sns\Message::fromRawPostData();
/*[OC> 11 */ $validator = new \Aws\Sns\MessageValidator();
/*[OC> 12 */ $validator->validate($message);
/*[OC> 13 */
/*[OC> 14 */ if (in_array($message['Type'], ['SubscriptionConfirmation', 'UnsubscribeConfirmation']) {
/*[OC> 15 */ file_get_contents($message['SubscribeURL']);
/*[OC> 16 */ }
/*[OC> 17 */
/*[OC> 18 */ $log = new SplFileObject('../messages.log', 'a');
/*[OC> 19 */ $log->fwrite($message['Message'] . "\n");
/*[OC> 20 */ } catch (Exception $e) {
/*[OC> 21 */ http_response_code(404);
/*[OC> 22 */ die;
/*[OC> 23 */ }
/*[OC> 24 */
/* rexNote->start *``~x&*``^
*
Conclusion
As you can see, receiving, verifying, and handling Amazon SNS messages is simple. Setting up your application to receive SNS messages will allow you to create applications that can handle asynchronous communication from AWS services and other parts of your application.
*
* -CHO
** rexNote->end *``^~x&*``^ */
/*
"Software as disruptive as our attitude"
~ http://disruptiveware.com
/**·*··*····+·+*··*····+···*····+···+*····+··*····+·+*··*····+···*····+···+*····+··+·+*··*····+···*····+···+*····+··*····+·+*··*····+···*····+···+*···*··*····+·+*··*····-+/
~+a-88RR88
,8P'8
888 d8888 8888888b. .d8888b. 8888888 d8"8D8
888 d88888 888 Y88b d88P Y88b 888 ,8P' 'I
888 d88P888 888 888 888 888 888 cd8" ' + Licensing: disruptiveware.com
888 d88P 888 888 d88P 888 888889_d8" + Author: Christopher D. Hogan, PhD. <[email protected]>
888 d88P 888 8888888P" 888 88888 888 .dB"
888 d88P 888 888 T88b 888 888 888 dB~ TM
888 d8888888888 888 T88b Y88b d88P 888 + Descriptor: ## Genus ##
88888888 d88P 888 888 T88b "Y8888P88 8888888888 + Version: 3.3.0.49 was the last source modification in GIT
+ Support: LargeCollab.com
+ Package: genus.rex.automotive
|**·*··*····+·+*··*····+···*····+···+*····+··*····+·+*··*····+···*····+···+*····+··+·+*··*····+···*····+···+*····+··*····+·+*··*····+···*····+···+*···*··*····+·+*··*····-+*/
// eof
/*+--------------------[objectcode: 12/20/2015 1:36:58 PM] [/end.automation]+--------------------+*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment