本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用第 3 AWS SDK for PHP 版管理 Amazon SNS 中的主題
若要將通知傳送至 Amazon Simple Queue Service (Amazon SQS)、HTTP/HTTPS URL、電子郵件或電子郵件AWS Lambda,您必須先建立一個主題來管理訊息傳遞給該主題的任何訂閱者。AWS SMS
就觀察者設計模式而言,主題即有如主旨。建立主題之後,您便要新增訂閱者,其將於訊息發佈至該主題時自動收到通知。
進一步了解如何訂閱使用第 3 AWS SDK for PHP 版在 Amazon SNS 中管理訂閱中的主題。
下列範例示範如何:
-
建立要將通知發佈至使用的主題CreateTopic。
-
使ListTopics用返回請求者的主題列表。
-
使用刪除主題及其所有訂閱DeleteTopic。
-
使用返回一個主題的所有屬性GetTopicAttributes。
-
允許主題擁有者使用將主題的屬性設定為新值SetTopicAttributes。
如需使用 Amazon SNS 的詳細資訊,請參閱 Amazon SNS 主題訊息傳遞狀態的屬性。
所有的範例程式碼都可以AWS SDK for PHP在這裡取
登入資料
在執行範例程式碼之前,請依照中的說明設定您的AWS認證憑證。然後匯入AWS SDK for PHP,如中所述基本使用。
建立主題
若要建立主題,請使用此CreateTopic作業。
您中的每個主題名稱都AWS 帳戶必須是唯一的。
匯入
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' ]); $topicname = 'myTopic'; try { $result = $SnSclient->createTopic([ 'Name' => $topicname, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
列出您的主題
若要列出目前「AWS區域」中最多 100 個現有主題,請使用此ListTopics作業。
匯入
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->listTopics(); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
刪除主題
若要移除現有主題及其所有訂閱,請使用此DeleteTopic作業。
凡是仍未交付予訂閱者的任何訊息都將一併刪除。
匯入
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' ]); $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()); }
取得主題屬性
若要擷取單一現有主題的屬性,請使用此GetTopicAttributes作業。
匯入
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' ]); $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()); }
設定主題屬性
若要更新單一現有主題的屬性,請使用此SetTopicAttributes作業。
您只能設定 Policy
、DisplayName
和 DeliveryPolicy
屬性。
匯入
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' ]); $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()); }