本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用管理 Amazon SNS 中的订阅Amazon SDK for PHP版本 3
使用 Amazon Simple Notification Service (Amazon SNS) 主题,将通知发送到 Amazon Simple Queue Service (Amazon SQS)、HTTP/HTTPS、电子邮件地址、Amazon Server Migration Service(Amazon SMS),或者Amazon Lambda.
订阅将附加到某个主题,该主题管理将消息发送给订阅者。了解有关创建主题的更多信息使用管理 Amazon SNS 中的主题Amazon SDK for PHP版本 3.
以下示例演示如何:
-
使用 Subscribe 订阅到现有主题。
-
使用 ConfirmSubscription 验证订阅。
-
使用 ListSubscriptionsByTopic 列出现有订阅。
-
使用 Unsubscribe 删除订阅。
-
使用 publish 将消息发送给某一主题的所有订阅者。
有关使用 Amazon SNS 的更多信息,请参阅使用 Amazon SNS 进行系统到系统消息收发.
Amazon SDK for PHPGitHub 上提供了
凭证
运行示例代码之前,请配置您的Amazon凭证,如中所述设置 凭证. 然后导入Amazon SDK for PHP,如中所述基本用法.
将电子邮件地址订阅到主题
要建立电子邮件地址订阅,请使用 Subscribe 操作。
您可以使用订阅模式,根据在所传递参数中使用的值,将多种不同的终端节点订阅到 Amazon SNS 主题。本主题中的其他示例演示了这一点。
在本示例中,终端节点是电子邮件地址。将向该电子邮件发送确认令牌。在收到电子邮件的 3 天内,使用此确认令牌验证订阅。
导入
require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$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\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$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 函数订阅,请使用订阅operation.
您可以使用订阅模式,根据在所传递参数中使用的值,将多种不同的终端节点订阅到 Amazon SNS 主题。本主题中的其他示例演示了这一点。
在此示例中,终端节点是 Lambda 函数。将会向此 Lambda 函数发送确认令牌。在收到电子邮件的 3 天内,使用此确认令牌验证订阅。
导入
require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$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 SNS 中发送短信Amazon SDK for PHP版本 3.
导入
require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$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\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$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->subscribe([ 'Token' => $subscription_token, 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
列出对主题的订阅
要列出指定的最多 100 个现有订阅Amazon区域,请使用ListSubscriptionsoperation.
导入
require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$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\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$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 主题的各个终端节点,请使用发布operation.
创建包含用于发布消息的参数的对象,包括消息文本以及 Amazon SNS 主题的 Amazon 资源名称 (ARN)。
导入
require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$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()); }