Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon SNS to push topic using PHP

I'm trying to understand amazon php sdk for AWS but I really can't use it. I find some basic classes to create, display and push topic but I doesn't work either. I just want to find a way (the simplest way) to push a topic from my website.

like image 471
Dar Avatar asked Mar 14 '12 06:03

Dar


2 Answers

First and foremost, to get acquainted with Amazon Simple Notification Service (SNS), I recommend to perform all required step manually once via the AWS Management Console as explained in the Getting Started Guide, i.e. Create a Topic, Subscribe to this Topic and Publish a message to it.

Afterwards it should be fairly straight forward to facilitate the sample fragments provided in the documentation of the AWS SDK for PHP running, see e.g. method publish() within class AmazonSNS:

$sns = new AmazonSNS();

// Get topic attributes
$response = $sns->publish(
    'arn:aws:sns:us-east-1:9876543210:example-topic',
    'This is my very first message to the world!',
    array(
        'Subject' => 'Hello world!'
    )
);

// Success?
var_dump($response->isOK());

For a more complete example you might want to check out the the example provided in Sending Messages Using SNS [...].

If none of this works out, you'll have to provide more details regarding the specific problems you are encountering, as requested by tster already.

Good luck!

like image 166
Steffen Opel Avatar answered Sep 27 '22 15:09

Steffen Opel


As told to you by @SteffenOpel, you should once try to perform all required steps manually once via the AWS Management Console.

Then you may use the AWS SDK for PHP(v3) as below to create the SNS client(or infact any service's client, surely with some changes) and then to create a SNS Topic.

<?php
    //assuming that use have downloaded the zip file for php sdk
    require 'C:/wamp/www/aws sdk/aws-autoloader.php'; //Change the path according to you
    use Aws\Sns\SnsClient;



    try{
            /*-------------METHOD 1----------------*/
            // Create a new Amazon SNS client using AWS v3
            //$sns = new Aws\Sns\SnsClient([
            $sns = new SnsClient([

                'region' => 'us-west-2', //Change according to you
                'version' => '2010-03-31', //Change according to you
                'credentials' => [
                    'key'    => '<Your root AWS Key',
                    'secret' => '<Your root AWS Secret>',
                ],
                'scheme' => 'http', //disables SSL certification, there was an error on enabling it 

            ]);

            $result = $sns -> createTopic([
            'Name' => '<Your Topic>',
            ]);


        /*-------------METHOD 2----------------*/
        /*
        // Create a new Amazon SNS client using AWS v2
        $sns = SnsClient::factory(array(

            'region' => 'us-west-2',
            'version' => '2010-03-31',
            'credentials' => [
                'key'    => '<Your root AWS Key',
                'secret' => '<Your root AWS Secret>',
            ],
            'scheme' => 'http',
        ));


        $result = $sns -> createTopic([
            'Name' => '<Your Topic>',
            ]);

        */



        /*-------------METHOD 3----------------*/
        /*
        // Create a new Amazon SNS client using AWS SDK class
        // Use the us-west-2 region and latest version of each client.
        $sharedConfig = [
            'region'  => 'us-west-2',
            'version' => '2010-03-31',

            'credentials' => [
                'key'    => '<Your root AWS Key',
                'secret' => '<Your root AWS Secret>',
            ],
            //'ssl.certificate_authority' => '/path/to/updated/cacert.pem',
            'scheme' => 'http',
        ];

        // Create an SDK class used to share configuration across clients.
        $sdk = new Aws\Sdk($sharedConfig);

        $sns =  $sdk -> createSns();

        $result = $sns -> createTopic([
            'Name' => '<Your Topic>',
            ]);
        */


        if ($result)
            echo "Yes";
        else
            echo "No";
    }

    catch(Exception $e){

        echo 'Caught Exception: ', $e->getMessage(), "\n";

    }


?>

NOTE: This code illustrates creating the client for SNS in three different methods. You may uncomment and use one according to your need. Method 1 (version 3) is the best if you're creating a single client, else use Method 3. Method 2 is soon gonna depreciate (as its version 2)

like image 22
h8pathak Avatar answered Sep 27 '22 15:09

h8pathak