SenderID using Amazon Web Services PHP SDK - php

I'm trying to send an SMS through my PHP site using the AWS SDK. I use the code from Sending SMS with Amazon AWS services PHP.
require $_SERVER['DOCUMENT_ROOT'] . '/inc/aws/aws-autoloader.php';
error_reporting(E_ALL);
ini_set("display_errors", 1);
$params = array(
'credentials' => array(
'key' => 'abc',
'secret' => 'abc',
),
'region' => 'eu-west-1', // < your aws from SNS Topic region
'version' => 'latest'
);
$sns = new \Aws\Sns\SnsClient($params);
$args = array(
"SenderID" => "TestSender",
"SMSType" => "Transactional",
"Message" => "Sending SMS with PHP",
"PhoneNumber" => "+87654321"
);
$result = $sns->publish($args);
echo "<pre>";
var_dump($result);
echo "</pre>";
This is not working. I have tested with a lot of different SenderIDs and all messages are received from NOTICE.
However, when I send a message from the AWS console, the message is received with the correct SenderID. So I assume my code is wrong.

I found the solution. Set the args this way. It works!
$args = array(
'MessageAttributes' => [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'YourSenderName'
]
],
"SMSType" => "Transactional",
"PhoneNumber" => "+87654321",
"Message" => "Hello World!"
);

Related

AMAZON SNS / SMS , sending sms , delivery status

I am using amazon PHP SDK to try to send sms I am using following code:
<?php
require 'vendor/autoload.php';
$sdk = new Aws\Sns\SnsClient([
'region' => 'eu-west-1',
'version' => 'latest',
'credentials' => ['key' => 'xxx', 'secret' => 'xxx']
]);
$result = $sdk->publish([
'Message' => 'This is a test message.',
'PhoneNumber' => '+123456789',
'MessageAttributes' => ['AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'testing sms'
]
]]);
print_r( $result );
My question is how do I get delivery Status back to an endpoint url HTTPS or HTTP?
like the sms got delivered or failed? any idea?
The only way to do this today is to leverage the CloudWatch API (e.g. https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_GetLogEvents.html) to read the logs delivered to your log groups

How to send an email through AWS simple email service(ses) using signature version 4 in PHP

Currently I'm sending email through AWS SDK SES signature 3 and got an email from Amazon to upgrade it to SES signature version 4. But where to add signature in AWS SDK? below is the current code that is being used to send emails.
<?php
//SES
$SESCredentials = array(
'key' => awsSESAccessKey,
'secret' => awsSESSecretKey
);
$SESClient = new SesClient([
// 'profile' => 'default',
'version' => AWS_SDK_VERSION,
'region' => AWS_REGION,
'http' => [
'verify' => AWS_CERT_PATH
],
'credentials' => $SESCredentials
]);
$result = $SESClient->sendEmail([
'Destination' => [
'ToAddresses' => $recipient_emails
],
'ReplyToAddresses' => [$sender_email],
'Source' => $sender_email,
'Message' => [
'Body' => [
'Html' => [
'Charset' => $char_set,
'Data' => $html_body
]
],
'Subject' => [
'Charset' => $char_set,
'Data' => $subject,
]
]
]);
?>
You can use this package as it supports signature 4
https://github.com/daniel-zahariev/php-aws-ses
$signature_version = SimpleEmailService::REQUEST_SIGNATURE_V4;
$ses = new SimpleEmailService('AccessKey', 'SecretKey', $region_endpoint,
$trigger_error, $signature_version);
$m->addTo('Recipient Name <recipient#example.com>');
$m->setFrom('Sender <user#example.com>');
$m->setSubject('Hello, world!');
$m->setMessageFromString('This is the message body.');
$ses = new SimpleEmailService('AccessKey', 'SecretKey');
print_r($ses->sendEmail($m));

aws-sdk-php v3 sns not working for me

I really want to publish a lambda subscriber to an SNS topic. It works in amazon console, but with php-sdk it does not. When I execute the php code and I look in CloudWatch, there's nothing (no response). Why my SNS did not work?
it is my code, but it does not working:
....
$credentials = new Credentials($access, $secret);
$this->aws_client = new SnsClient(
array(
'region' => 'us-east-1',
'version' => 'latest',//'version' => '2010-03-31',
'credentials' => $credentials
)
);
...
$message = "Hello H.";
$message_atr = array('String' => array('DataType' => 'String', 'StringValue' => $message));
$payload = array(
'TargetArn' => 'arn:aws:sns:us-east-1:****:mysns',
'Message' => $message,
'MessageAttributes' => $message_atr,
);
$this->aws_client->publish($payload);

Send sms to Indian numbers using aws sns

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.
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);
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.

[AWS][DynamoDB] Connection to the DB through the PhP SDK

I am new to AWS. I have installed an EC2 server which processes PhP code. I am able to administrate the DB through the Amazon website. I'm trying to access my DynamoDB table with the following code:
use Aws\DynamoDb\DynamoDbClient;
try {
$client = DynamoDbClient::factory(array(
'profile' => 'default', // access ID + secret are in the .aws/credentials file
'region' => Region::EU_WEST_1 // also tried with "eu-west-1"
));
echo "after client instanciation"; // this is not displayed
$response = $client->getItem([
'TableName' => 'Child',
'Key' => [
'ChildID' => 'Nicolas'
]
]);
print_r ($response['Item']);
} catch (Exception $e) {
echo '<p>Exception received : ', $e->getMessage(), "\n</p>";
}
I'm not getting any exception. The child I'm trying to get isn't displayed (I did create it). Also tried with the putItem method but it didn't add anything to the DB.
I think you are missing the data type in the key:
$response = $dynamodb->getItem([
'TableName' => 'Child',
'Key' => [
'ChildID' => [ 'S' => 'Nicolas' ]
]
]);
Now you should get the output, you can refer this Link.
Try using the following code, it allows you to pass public and secret keys through parameters.
$client = new DynamoDbClient([
'version' => 'latest',
'region' => 'ap-northeast-1',
'credentials' => [
'key' => 'A5ITUTLAK7W47NNNNQ',
'secret' => 'DrsEjmEMs4PUPIY5/12a/cpUB7JVVcKLahFz826p'
]
]);
try {
$result = $client->getItem(array(
'ConsistentRead' => true,
'TableName' => 'fruits',
'Key' => array(
'id' => array('S' => '1')
)
));
Replace the keys with yours.
Reference:
https://solutionarchitects.guru/viewtopic.php?f=30&t=27

Categories