The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for PHP with Amazon SNS.
Actions are code excerpts that show you how to call individual service functions.
Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.
Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.
Topics
The following code example shows how to check whether a phone number is opted out of receiving Amazon SNS messages.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Indicates whether the phone number owner has opted out of receiving SMS messages from your AWS SNS account.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
$phone = '+1XXX5550100';
try {
$result = $SnSclient->checkIfPhoneNumberIsOptedOut([
'phoneNumber' => $phone,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
- For more information, see AWS SDK for PHP Developer Guide.
- For API details, see CheckIfPhoneNumberIsOptedOut in AWS SDK for PHP API Reference.
The following code example shows how to confirm the owner of an endpoint wants to receive Amazon SNS messages by validating the token sent to the endpoint by an earlier Subscribe action.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Verifies an endpoint owner's intent to receive messages by validating the token sent to the endpoint by an earlier Subscribe action.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
$subscription_token = 'arn:aws:sns:us-east-1:111122223333:MyTopic:123456-abcd-12ab-1234-12ba3dc1234a';
$topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic';
try {
$result = $SnSclient->confirmSubscription([
'Token' => $subscription_token,
'TopicArn' => $topic,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
- For API details, see ConfirmSubscription in AWS SDK for PHP API Reference.
The following code example shows how to create an Amazon SNS topic.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Create a Simple Notification Service topics in your AWS account at the requested region.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$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());
}
- For more information, see AWS SDK for PHP Developer Guide.
- For API details, see CreateTopic in AWS SDK for PHP API Reference.
The following code example shows how to delete an Amazon SNS subscription.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Deletes a subscription to an Amazon SNS topic.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
$subscription = 'arn:aws:sns:us-east-1:111122223333:MySubscription';
try {
$result = $SnSclient->unsubscribe([
'SubscriptionArn' => $subscription,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
- For more information, see AWS SDK for PHP Developer Guide.
- For API details, see Unsubscribe in AWS SDK for PHP API Reference.
The following code example shows how to delete an Amazon SNS topic and all subscriptions to that topic.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Deletes a SNS topic and all its subscriptions.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$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());
}
- For API details, see DeleteTopic in AWS SDK for PHP API Reference.
The following code example shows how to get the properties of an Amazon SNS topic.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
$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());
}
- For API details, see GetTopicAttributes in AWS SDK for PHP API Reference.
The following code example shows how to get the settings for sending Amazon SNS SMS messages.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Get the type of SMS Message sent by default from the AWS SNS service.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
try {
$result = $SnSclient->getSMSAttributes([
'attributes' => ['DefaultSMSType'],
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
- For more information, see AWS SDK for PHP Developer Guide.
- For API details, see GetSMSAttributes in AWS SDK for PHP API Reference.
The following code example shows how to list phone numbers that are opted out of receiving Amazon SNS messages.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Returns a list of phone numbers that are opted out of receiving SMS messages from your AWS SNS account.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
try {
$result = $SnSclient->listPhoneNumbersOptedOut([
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
- For more information, see AWS SDK for PHP Developer Guide.
- For API details, see ListPhoneNumbersOptedOut in AWS SDK for PHP API Reference.
The following code example shows how to retrieve the list of subscribers of an Amazon SNS topic.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Returns a list of Amazon SNS subscriptions in the requested region.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
try {
$result = $SnSclient->listSubscriptions([
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
- For API details, see ListSubscriptions in AWS SDK for PHP API Reference.
The following code example shows how to list Amazon SNS topics.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Returns a list of the requester's topics from your AWS SNS account in the region specified.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$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());
}
- For API details, see ListTopics in AWS SDK for PHP API Reference.
The following code example shows how to publish SMS messages using Amazon SNS.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Sends a a text message (SMS message) directly to a phone number using Amazon SNS.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
$message = 'This message is sent from a Amazon SNS code sample.';
$phone = '+1XXX5550100';
try {
$result = $SnSclient->publish([
'Message' => $message,
'PhoneNumber' => $phone,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
- For more information, see AWS SDK for PHP Developer Guide.
- For API details, see Publish in AWS SDK for PHP API Reference.
The following code example shows how to publish messages to an Amazon SNS topic.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Sends a message to an Amazon SNS topic.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
$message = 'This message is sent from a Amazon SNS code sample.';
$topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic';
try {
$result = $SnSclient->publish([
'Message' => $message,
'TopicArn' => $topic,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
- For more information, see AWS SDK for PHP Developer Guide.
- For API details, see Publish in AWS SDK for PHP API Reference.
The following code example shows how to set the default settings for sending SMS messages using Amazon SNS.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
try {
$result = $SnSclient->SetSMSAttributes([
'attributes' => [
'DefaultSMSType' => 'Transactional',
],
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
- For more information, see AWS SDK for PHP Developer Guide.
- For API details, see SetSmsAttributes in AWS SDK for PHP API Reference.
The following code example shows how to set Amazon SNS topic attributes.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Configure the message delivery status attributes for an Amazon SNS Topic.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$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());
}
- For API details, see SetTopicAttributes in AWS SDK for PHP API Reference.
The following code example shows how to subscribe an HTTP or HTTPS endpoint so it receives notifications from an Amazon SNS topic.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Prepares to subscribe an endpoint by sending the endpoint a confirmation message.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
$protocol = 'https';
$endpoint = 'https://';
$topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic';
try {
$result = $SnSclient->subscribe([
'Protocol' => $protocol,
'Endpoint' => $endpoint,
'ReturnSubscriptionArn' => true,
'TopicArn' => $topic,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
- For API details, see Subscribe in AWS SDK for PHP API Reference.
The following code example shows how to subscribe an email address to an Amazon SNS topic.
SDK for PHP
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
/**
* Prepares to subscribe an endpoint by sending the endpoint a confirmation message.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
$protocol = 'email';
$endpoint = 'sample@example.com';
$topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic';
try {
$result = $SnSclient->subscribe([
'Protocol' => $protocol,
'Endpoint' => $endpoint,
'ReturnSubscriptionArn' => true,
'TopicArn' => $topic,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
- For API details, see Subscribe in AWS SDK for PHP API Reference.