AWS SDK for PHP
Developer Guide

Managing Topics in Amazon SNS with the AWS SDK for PHP Version 3

To send notifications to Amazon Simple Queue Service (Amazon SQS), HTTP/HTTPS URLs, email, AWS SMS, or AWS Lambda, you must first create a topic that manages the delivery of messages to any subscribers of that topic.

In terms of the observer design pattern, a topic is like the subject. After a topic is created, you add subscribers that are notified automatically when a message is published to the topic.

Learn more about subscribing to topics in Managing Subscriptions in Amazon SNS with AWS SDK for PHP Version 3.

The following examples show how to:

  • Create a topic to publish notifications to using CreateTopic.

  • Return a list of the requester's topics using ListTopics.

  • Delete a topic and all of its subscriptions using DeleteTopic.

  • Return all of the properties of a topic using GetTopicAttributes.

  • Allow a topic owner to set an attribute of the topic to a new value using SetTopicAttributes.

For more information about using Amazon SNS, see Amazon SNS Topic Attributes for Message Delivery Status.

All the example code for the AWS SDK for PHP Version 3 is available here on GitHub.

Credentials

Before running the example code, configure your AWS credentials, as described in Credentials for the AWS SDK for PHP Version 3. Then import the AWS SDK for PHP, as described in Basic Usage Patterns of the AWS SDK for PHP Version 3.

Create a Topic

To create a topic, use the CreateTopic operation.

Each topic name in your AWS account must be unique.

Imports

require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $topicname = 'myTopic'; try { $result = $SnSclient->createTopic([ 'Name' => $topicname, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

List Your Topics

To list up to 100 existing topics in the current AWS Region, use the ListTopics operation.

Imports

require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); try { $result = $SnSclient->listTopics([ ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Delete a Topic

To remove an existing topic and all of its subscriptions, use the DeleteTopic operation.

Any messages that have not been delivered yet to subscribers will also be deleted.

Imports

require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->deleteTopic([ 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Get Topic Attributes

To retrieve properties of a single existing topic, use the GetTopicAttributes operation.

Imports

require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->getTopicAttributes([ 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Set Topic Attributes

To update properties of a single existing topic, use the SetTopicAttributes operation.

You can set only the Policy, DisplayName, and DeliveryPolicy attributes.

Imports

require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $attribute = 'Policy | DisplayName | DeliveryPolicy'; $value = 'First Topic'; $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->setTopicAttributes([ 'AttributeName' => $attribute, 'AttributeValue' => $value, 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }