-1

I am trying to send a sms to a particular number using aws sns using the following code but it requires a target/topic arn which is not applicable for me as I want to send sms to the numbers that I set in the params.

$client = SnsClient::factory([
        'credentials' => [
            'key'    => "my key",
            'secret' => "my secret"
        ],
        'region'  => "us-east-1",
        'version' => "latest"
    ]);

    $sent = $client->publish(
        [

            'Message' => 'This is the message',
            'PhoneNumber' => '+91887******7'
        ]
    );

I am not able to find the exact params for the publish object.

Harshit
  • 711
  • 1
  • 9
  • 29

2 Answers2

4

I had to update the aws php sdk (1). Then the following code works fine -

$sns = SnsClient::factory(array(
        'credentials' => array(
            'key' => 's3_key',
            'secret' => 's3_secret'
        ),
        'region' => Constants::$s3Region,
        'version' => 'latest'
    ));

$msgattributes = [
        'AWS.SNS.SMS.SenderID' => [
            'DataType' => 'String',
            'StringValue' => 'Klassroom',
        ],
        'AWS.SNS.SMS.SMSType' => [
            'DataType' => 'String',
            'StringValue' => 'Transactional',
        ]
    ];

    $payload = array(
        'Message' => $message,
        'PhoneNumber' => $number,
        'MessageAttributes' => $msgattributes
    );

    $result=$sns->publish($payload);
Harshit
  • 711
  • 1
  • 9
  • 29
2

From the Amazon SNS Publish() documentation for PHP:

  • TargetArn: If you don't specify a value for the TargetArn parameter, you must specify a value for the PhoneNumber or TopicArn parameters.
  • TopicArn: If you don't specify a value for the TopicArn parameter, you must specify a value for the PhoneNumber or TargetArn parameters.
  • PhoneNumber: If you don't specify a value for the PhoneNumber parameter, you must specify a value for the TargetArn or TopicArn parameters.

Therefore, provide a PhoneNumber instead of a Target/Topic.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470