I've download the aws.zip file for AWS V3 SDK for PHP and extracted it into my project's folder and connected it's auto-loader into my auto-loader. I've also gone though all of the steps to create an AWS account, along with setting up SNS including acquiring a phone number to use. All of the examples work has shown in the sanity check.
There is a little dark corner that you should know about. You need to create a credentials
file in your ~/.aws/
directory. If you are using this from a php-fpm context, that home directory might be your /var/www/
directory so you should put your credential files under /var/www/.aws/
.
More information on the configuration file can be found here ... AWS Command Line Interface - Configuration and credential file settings.
Following the examples from the AWS SNS Documentation - Publish to a Text Message (SMS Message).
Test-AWS-SNS.php
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.';
$phone = '+1AAALLL####';
try {
$result = $SnSclient->publish([
'Message' => $message,
'PhoneNumber' => $phone,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
(Notes: +1AAALLL####
actually points to my cell phone number, but I'm obviously not going to put that here.)
The problem is that there is no documentation for how to send a text message from one of the numbers that I own from AWS' SNS Long Code list. It always comes from the first phone number in the list and I can't find any documentation on how to change the number to another one. Some help here would be great.
Obviously, I've been all over the documentation and searched Stack Overflow as well. This one is pretty close, as it sets a SenderID. Their documentation mentions how to do it as an optional number 9 next to the sender ID from above But I do know that it is possible, because they added the feature on Oct 23, 2020 - Amazon SNS now supports selecting the origination number when sending SMS messages -- They just haven't put any code samples.
Help me Obi-Wan Kenobi, you're my only hope.
So the solutions to this, after much looking around and rubber ducking the problem while typing into the text box above, is to use a couple of different answers together. The Stack Overflow thread mentioned in prior research about setting the SenderID was very helpful to atleast getting the raw information into the AWS SDK.
$params = [
'credentials' => [
'key' => 'iam_key',
'secret' => 'iam_secret',
],
'region' => 'ap-south-1', // < your aws from SNS Topic region
'version' => 'latest',
'http' => ['verify' => false]
];
$sns = \Aws\Sns\SnsClient::factory($params);
$msgattributes = [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'Klassroom',
],
'AWS.SNS.SMS.SMSType' => [
'DataType' => 'String',
'StringValue' => 'Transactional',
]
];
$payload = [
'Message' => 'HK test',
'PhoneNumber' => '1234567890',
'MessageAttributes' => $msgattributes
];
$result = $sns->publish($payload);
If you scroll into the SMS Publish To Phone documentation, you'll see the string used above AWS.SNS.SMS.SenderID
and the next item down is ... AWS.MM.SMS.OriginationNumber
... So maybe that will work?
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'
]);
$msgAttributes = [
'AWS.MM.SMS.OriginationNumber' => [
'DataType' => 'String',
'StringValue' => '+1XXXYYYZZZZ',
]
];
$message = 'This message is sent from a Amazon SNS code sample.';
$phone = '+1AAALLL####';
try {
$result = $SnSclient->publish([
'Message' => $message,
'PhoneNumber' => $phone,
'MessageAttributes' => $msgAttributes
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
Note: Obviously, +1XXXYYYZZZZ
is a number from my already established long code list, and just like before +1AAALLL####
is my phone number or the phone number of the person I want to send the text message to.