Sending SmS using Aws - php

I am implementing an SMS sending server, for that I am using AWS. I have already used their documentation to carry out all the configuration and installation. I have also placed the files and SDK on the AWS server to run the application. But when running the application, I have no error return, but the SMS does not arrive either.
My code is:
public function SendAwsSms(){
// Passing Aws\Credentials\AssumeRoleCredentialProvider options directly
$profile = new InstanceProfileProvider();
$ARN = "arnkeyprovidedfromaws";
$sessionName = "nameaccessrandom";
$assumeRoleCredentials = new AssumeRoleCredentialProvider([
'client' => new StsClient([
'region' => 'sa-east-1',
'version' => 'latest',
'credentials' => $profile
]),
'assume_role_params' => [
'RoleArn' => $ARN,
'RoleSessionName' => $sessionName,
],
]);
$provider = CredentialProvider::memoize($assumeRoleCredentials);
$SnSclient = new SnsClient([
'region' => 'sa-east-1',
'version'=> 'latest',
]);
try {
$result = $SnSclient->publish([
"SenderID"=>"Idsender",
"SMSType"=>"Promocional",
"Message"=>"Message test",
"PhoneNumber"=>"+1111111111111",
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
}
When I run the code, no error is displayed, but the SMS does not arrive at the destination.
Can someone show me what's wrong in there? I using PHP, with the CodeIgniter framework, and my system is located in AWS servers.

Related

AWS - Cognito - AdminSetUserPassword returns "Operation not found: AdminSetUserPassword"

I am trying to set a password for a Cognito user that I have already created using the AdminSetUserPassword function, but I keep getting "Operation not found: AdminSetUserPassword" as response.
I have checked the version of CognitoIdentityProvider ('2016-04-18'), which doesn't seem to be the problem. I have also tried to go around it using AdminInitiateAuth and AdminRespondToAuthChallenge, but the problem with the InitiateAuth function is that I get no 'Session' in response!
Any help would be greatly appreciated!
$args = [
'credentials' => *CREDENTIALS*,
'region' => 'eu-central-1',
'version' => 'latest',
'app_client_id' => *CLIENT_ID*,
'user_pool_id' => *USER_POOL_ID*,
];
$client = new CognitoIdentityProviderClient($args);
try {
$response = $client->adminSetUserPassword([
"Password" => $new_password,
"Permanent" => true,
"Username" => *USER_USERNAME*,
"UserPoolId" => *USER_POOL_ID*
]);
return true;
} catch (Exception $e) {
return false;
}
AdminSetUserPassword was only added to the aws/aws-sdk-php package in version 3.93.3. You can update your version of the package with composer require aws/aws-sdk-php:">3.93.3".

Amazon SNS - aws-sdk-php

$accessKey = 'XZA...';
$accessSecret = 'YKW...';
$credentials = new Aws\Credentials\Credentials($accessKey, $accessSecret);
$sharedConfig = [
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => $credentials
];
$sdk = new Aws\Sdk($sharedConfig);
$sns = new SnsClient($sharedConfig);
$payload = [
'PhoneNumber' => '+999999999', // E.164 format
'Message' => md5(time()),
'MessageAttributes' => [
'DefaultSenderID' => ['DataType'=>'String','StringValue'=>'MyBrandName'],
'DefaultSMSType' => ['DataType'=>'String','StringValue'=>'Transactional']
]
];
try {
$data = $sns->publish( $payload );
$MessageId = $data->get('MessageId');
} catch ( Exception $e ) { }
I'm using the AWS SDK for PHP - Version 3.
The code above works well when i'm sending a single SMS message except the attribute DefaultSenderID wich is not working when i send a SMS to a mobile device.
Amazon documentation says that DefaultSenderID – A string, such as your business brand, that is displayed as the sender on the receiving device. Support for sender IDs varies by country. The sender ID can be 1 - 11 alphanumeric characters, and it must contain at least one letter.
Anyone has experienced this problem using the Amazon SNS?
For anybody still struggling with this.
If you look at the documentation here, you will find that you need to add the key AWS.SNS.SMS.SenderID to the payload's MessageAttributes.
The following should work:
$payload = [
'PhoneNumber' => '+999999999', // E.164 format
'Message' => md5(time()),
'MessageAttributes' => [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'YourSenderID',
]
]
];
try {
$data = $sns->publish($payload);
$MessageId = $data->get('MessageId');
} catch (Exception $e) { }

Laravel AWS PHP SDK - CreatePlatformEndpoint throws 400 Bad Request

I am using "aws/aws-sdk-php-laravel": "~3.0"
When i am trying to register the android device its throwing the error.
Below is the snapshot of it.
I tried with 2 different code formats but both gives the same exception.
$device = 'gcm_id_here';
$sns = App::make('aws')->createClient('sns');
$result = $sns->createPlatformEndpoint(['Token' => 'device_unique_id',
'PlatformApplicationArn' => $device]);
// or
$credentials = new Credentials('key', 'secret');
$client = new SnsClient([
'version' => 'latest',
'region' => 'us-west-2',
'credentials' => $credentials
]);
$SNSEndPointData = $client->createPlatformEndpoint([
'PlatformApplicationArn' => $device,
'Token' => 'device_unique_id'
]);
Please help me solve this issue.

Amazon SNS using PHP SDK - createPlatformEndpoint "InvalidArgumentException" exception

I'm using latest PHP SDK(V3) for Amazon SNS. I have a problem when I'm trying to create an endpoint for a platform (Registering user devices to a platform). The error says I have an "InvalidArgumentException", but I have double checked with the document and I'm passing correct arguments. Please find below my code.
try {
$credentials = new Credentials($SNS_ACCESS_KEY, $SNS_SECRET_KEY);
$s3Client = new S3Client([
'version' => 'latest',
'region' => 'us-west-2',
'credentials' => $credentials
]);
$SNSEndPointData = $s3Client->createPlatformEndpoint([
'PlatformApplicationArn' => $SNS_APP_ARN,
'Token' => $device_token
]);
}
catch(exception $e) {
print $e->__toString();
}
If anyone can help or point me to right direction it is highly appreciated.
Here is the full answer just in case if anyone interested,
require 'vendor/autoload.php';
use Aws\Credentials\Credentials;
use Aws\Sns\SnsClient;
try {
$credentials = new Credentials($SNS_ACCESS_KEY, $SNS_SECRET_KEY);
$client = new SnsClient([
'version' => 'latest',
'region' => 'us-west-2',
'credentials' => $credentials
]);
$SNSEndPointData = $client->createPlatformEndpoint([
'PlatformApplicationArn' => $SNS_APP_ARN,
'Token' => 'phone token'
]);
print $SNSEndPointData;
}
catch(exception $e) {
$message = $e->getMessage();
print $message;
}

Amazon SNS to push topic using PHP

I'm trying to understand amazon php sdk for AWS but I really can't use it.
I find some basic classes to create, display and push topic but I doesn't work either.
I just want to find a way (the simplest way) to push a topic from my website.
First and foremost, to get acquainted with Amazon Simple Notification Service (SNS), I recommend to perform all required step manually once via the AWS Management Console as explained in the Getting Started Guide, i.e. Create a Topic, Subscribe to this Topic and Publish a message to it.
Afterwards it should be fairly straight forward to facilitate the sample fragments provided in the documentation of the AWS SDK for PHP running, see e.g. method publish() within class AmazonSNS:
$sns = new AmazonSNS();
// Get topic attributes
$response = $sns->publish(
'arn:aws:sns:us-east-1:9876543210:example-topic',
'This is my very first message to the world!',
array(
'Subject' => 'Hello world!'
)
);
// Success?
var_dump($response->isOK());
For a more complete example you might want to check out the the example provided in Sending Messages Using SNS [...].
If none of this works out, you'll have to provide more details regarding the specific problems you are encountering, as requested by tster already.
Good luck!
As told to you by #SteffenOpel, you should once try to perform all required steps manually once via the AWS Management Console.
Then you may use the AWS SDK for PHP(v3) as below to create the SNS client(or infact any service's client, surely with some changes) and then to create a SNS Topic.
<?php
//assuming that use have downloaded the zip file for php sdk
require 'C:/wamp/www/aws sdk/aws-autoloader.php'; //Change the path according to you
use Aws\Sns\SnsClient;
try{
/*-------------METHOD 1----------------*/
// Create a new Amazon SNS client using AWS v3
//$sns = new Aws\Sns\SnsClient([
$sns = new SnsClient([
'region' => 'us-west-2', //Change according to you
'version' => '2010-03-31', //Change according to you
'credentials' => [
'key' => '<Your root AWS Key',
'secret' => '<Your root AWS Secret>',
],
'scheme' => 'http', //disables SSL certification, there was an error on enabling it
]);
$result = $sns -> createTopic([
'Name' => '<Your Topic>',
]);
/*-------------METHOD 2----------------*/
/*
// Create a new Amazon SNS client using AWS v2
$sns = SnsClient::factory(array(
'region' => 'us-west-2',
'version' => '2010-03-31',
'credentials' => [
'key' => '<Your root AWS Key',
'secret' => '<Your root AWS Secret>',
],
'scheme' => 'http',
));
$result = $sns -> createTopic([
'Name' => '<Your Topic>',
]);
*/
/*-------------METHOD 3----------------*/
/*
// Create a new Amazon SNS client using AWS SDK class
// Use the us-west-2 region and latest version of each client.
$sharedConfig = [
'region' => 'us-west-2',
'version' => '2010-03-31',
'credentials' => [
'key' => '<Your root AWS Key',
'secret' => '<Your root AWS Secret>',
],
//'ssl.certificate_authority' => '/path/to/updated/cacert.pem',
'scheme' => 'http',
];
// Create an SDK class used to share configuration across clients.
$sdk = new Aws\Sdk($sharedConfig);
$sns = $sdk -> createSns();
$result = $sns -> createTopic([
'Name' => '<Your Topic>',
]);
*/
if ($result)
echo "Yes";
else
echo "No";
}
catch(Exception $e){
echo 'Caught Exception: ', $e->getMessage(), "\n";
}
?>
NOTE: This code illustrates creating the client for SNS in three different methods.
You may uncomment and use one according to your need.
Method 1 (version 3) is the best if you're creating a single client, else use Method 3. Method 2 is soon gonna depreciate (as its version 2)
I success to do it by using this classes->
Amazon-SNS-client-for-PHP
Very good, easy to use and working just great.
According to the AWS official docs here, we will have to create a SnsClient and call it's publish method. You can get topic's ARN from AWS console.
$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());
}

Categories