在使用 Amazon SDK for PHP 版本 3 的 Amazon SNS 中管理订阅 - Amazon SDK for PHP
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在使用 Amazon SDK for PHP 版本 3 的 Amazon SNS 中管理订阅

使用 Amazon Simple Notification Service (Amazon SNS) 主题来将通知发送到 Amazon Simple Queue Service (Amazon SQS)、HTTP/HTTPS、电子邮件地址、Amazon Server Migration Service (Amazon SMS) 或 Amazon Lambda。

订阅将附加到某个主题,该主题管理将消息发送给订阅者。在在使用 Amazon SDK for PHP 版本 3 的 Amazon SNS 中管理主题中了解有关创建主题的更多信息。

以下示例演示如何:

有关使用 Amazon SNS 的更多信息,请参阅使用 Amazon SNS 进行系统到系统消息收发

的所有示例代码都可以在此Amazon SDK for PHP处找到 GitHub

凭证

运行示例代码之前,请配置您的 Amazon 凭证,如 凭证 中所述。然后导入 Amazon SDK for PHP,如 基本用法 中所述。

为主题订阅电子邮件地址

要建立电子邮件地址订阅,请使用 Subscribe 操作。

您可以使用订阅方法,根据在所传递参数中使用的值,将多种不同的端点订阅到某个 Amazon SNS 主题。本主题中的其他示例演示了这一点。

在本示例中,端点是电子邮件地址。将向该电子邮件发送确认令牌。在收到电子邮件的 3 天内,使用此确认令牌验证订阅。

导入

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

示例代码

$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()); }

将应用程序端点订阅到主题

要建立 Web 应用程序订阅,请使用 Subscribe 操作。

您可以使用订阅方法,根据在所传递参数中使用的值,将多种不同的端点订阅到某个 Amazon SNS 主题。本主题中的其他示例演示了这一点。

在此示例中,端点是 URL。将会向此 Web 地址发送确认令牌。在收到电子邮件的 3 天内,使用此确认令牌验证订阅。

导入

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

示例代码

$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()); }

向主题订阅 Lambda 函数

要建立 Lambda 函数订阅,请使用 Subscribe 操作。

您可以使用订阅方法,根据在所传递参数中使用的值,将多种不同的端点订阅到某个 Amazon SNS 主题。本主题中的其他示例演示了这一点。

在此示例中,端点是 Lambda 函数。将会向此 Lambda 函数发送确认令牌。在收到电子邮件的 3 天内,使用此确认令牌验证订阅。

导入

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

示例代码

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $protocol = 'lambda'; $endpoint = 'arn:aws:lambda:us-east-1:123456789023:function:messageStore'; $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()); }

将文本 SMS 订阅到主题

要同时将 SMS 消息发送到多个电话号码,请将各个号码订阅到主题。

要建立电话号码订阅,请使用 Subscribe 操作。

您可以使用订阅方法,根据在所传递参数中使用的值,将多种不同的端点订阅到某个 Amazon SNS 主题。本主题中的其他示例演示了这一点。

在此示例中,端点是 E.164 格式的电话号码,这是国际电信的标准。

将会向此电话号码发送确认令牌。在收到电子邮件的 3 天内,使用此确认令牌验证订阅。

有关使用 Amazon SNS 发送 SMS 消息的替代方法,请参阅在使用 Amazon SDK for PHP 版本 3 的 Amazon SNS 中发送 SMS 消息

导入

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

示例代码

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $protocol = 'sms'; $endpoint = '+1XXX5550100'; $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()); }

确认订阅到主题

要实际创建订阅,端点所有者必须使用在最初建立订阅时发送的令牌,确认接收来自该主题的消息的意图,如前所述。确认令牌有效期为 3 天。3 天之后,您可以通过创建新订阅来重新发送令牌。

要确认订阅,请使用ConfirmSubscription操作。

导入

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

示例代码

$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()); }

列出对主题的订阅

要列出给定Amazon区域中最多 100 个现有订阅,请使用ListSubscriptions操作。

导入

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

示例代码

$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()); }

取消订阅主题

要删除订阅到某个主题的端点,请使用 Unsubscribe 操作。

如果订阅需要身份验证才能删除,则只有订阅的所有者或主题的所有者才可以取消订阅,并且需要 Amazon 签名。如果取消订阅调用无需身份验证,并且请求者不是订阅所有者,则会将一条最终取消消息传送到端点。

导入

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

示例代码

$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()); }

向 Amazon SNS 主题发布消息

要将消息发送到订阅到某个 Amazon SNS 主题的各个端点,请使用 Publish 操作。

创建包含用于发布消息的参数的对象,包括消息文本以及 Amazon SNS 主题的 Amazon 资源名称 (ARN)。

导入

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

示例代码

$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()); }