Search code examples
phpamazon-web-servicessmsamazon-sns

How to subscribe multiple phone numbers to a topic under Amazon SNS?


Amazon provides the php example code below to subscribe a number to a topic. However, that only adds one number at a time.

How can I add multiple numbers ($endpoint) to the same topic? What would the code need to be at the end?

PHP Example Code:

<?php
// snippet-start:[sns.php.subscribe_text_sms.complete]
// snippet-start:[sns.php.subscribe_text_sms.import]
require 'vendor/autoload.php';

use Aws\Sns\SnsClient; 
use Aws\Exception\AwsException;
// snippet-end:[sns.php.subscribe_text_sms.import]

/**
 * 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
 */
 
// snippet-start:[sns.php.subscribe_text_sms.main]
$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());
} 
// snippet-end:[sns.php.subscribe_text_sms.main]
// snippet-end:[sns.php.subscribe_text_sms.complete]
// snippet-sourcedescription:[SubscribeTextSMS.php demonstrates how to send a confirmation message as a text message.]

Solution

  • The solution was actually pretty simple. I just needed to add a list of numbers (through an array) and create a foreach loop:

    <?php
    // snippet-start:[sns.php.subscribe_text_sms.complete]
    // snippet-start:[sns.php.subscribe_text_sms.import]
    require 'vendor/autoload.php';
    
    use Aws\Sns\SnsClient; 
    use Aws\Exception\AwsException;
    // snippet-end:[sns.php.subscribe_text_sms.import]
    
    /**
     * 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
     */
     
    // snippet-start:[sns.php.subscribe_text_sms.main]
    $SnSclient = new SnsClient([
        'profile' => 'default',
        'region' => 'us-east-1',
        'version' => '2010-03-31'
    ]);
    
    $protocol = 'sms';
    $endpoints = array('+1XXX5550100', '+2XXX5550100', '+3XXX5550100');
    $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic';
    
    foreach ($endpoints as $endpoint) {
        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());
        } 
    }
    // snippet-end:[sns.php.subscribe_text_sms.main]
    // snippet-end:[sns.php.subscribe_text_sms.complete]
    // snippet-sourcedescription:[SubscribeTextSMS.php demonstrates how to send a confirmation message as a text message.]