I am implementing a free SMS API directly inside my STAccounts class for the first time tonight. The last update was token verification & SMTP API notifications (more than 3 login failures, etc).
I did google search, found them SMS.To as the only service that actually sent a SMS to my iPhone from my cURL test PHP script. All others failed, or cost money or disallowed daytime SMS sending unless "Pro" plan. Wtf?
No one has posted or shared any guide or anything at all on getting it running let alone a PHP class - so I do from scratch.
I figure its time to get with the times and consider security a little more. Tokens are nice, but tell that to the big sites out there.
The point of this is to send a 5-digit numerical PIN to a members smart phone as a SMS message from a page form (or login form), and accept the generated PIN as a return entry to the forms input text field, and doing somethign like string comparison according to (you can store the PIN in a table and remove row after verify), allowing whatever it is you wish to do afterwards once that JSON response comes back as "success". To me, it makes sense to just add a field in `users` table something like
And check that on site and do whatever if that says "yes" for logged in user (show it in account page or something).
We just verify the element with json_decode($something, true) and read the value. SMS.To returns JSON status response back to browser if the SMS to the members phone number was successful or not.
I wrote this and used the API documentation. This may help someone implement in their own script. I've laid it all out for easy porting. Works fine for me.
Define the constants:
Set the constants:
Checking and performing next step
Response:
{
"message": "Message is queued for sending! Please check report for update",
"success": true,
"message_id": "e7745289-7236-497f-acf2-f9cfd6a86f16"
}
Keep in mind, $securepin is not set because you can do it yourself - this is just to get you started. I use my own way.
I did google search, found them SMS.To as the only service that actually sent a SMS to my iPhone from my cURL test PHP script. All others failed, or cost money or disallowed daytime SMS sending unless "Pro" plan. Wtf?
No one has posted or shared any guide or anything at all on getting it running let alone a PHP class - so I do from scratch.
I figure its time to get with the times and consider security a little more. Tokens are nice, but tell that to the big sites out there.
The point of this is to send a 5-digit numerical PIN to a members smart phone as a SMS message from a page form (or login form), and accept the generated PIN as a return entry to the forms input text field, and doing somethign like string comparison according to (you can store the PIN in a table and remove row after verify), allowing whatever it is you wish to do afterwards once that JSON response comes back as "success". To me, it makes sense to just add a field in `users` table something like
ALTER TABLE users ADD 2fa_auth enum('yes','no') NOT NULL;
And check that on site and do whatever if that says "yes" for logged in user (show it in account page or something).
We just verify the element with json_decode($something, true) and read the value. SMS.To returns JSON status response back to browser if the SMS to the members phone number was successful or not.
I wrote this and used the API documentation. This may help someone implement in their own script. I've laid it all out for easy porting. Works fine for me.
Define the constants:
class myClass {
const DEF_SMS_API_HOST = 'api.sms.to/sms/send';
const DEF_SMS_API_KEY = 'Your_Own_API_Key';
const DEF_SMS_SENDER_ID = 'SMSto';
Set the constants:
$this->smsApiHost = self::DEF_SMS_API_HOST;
$this->smsApiKey = self::DEF_SMS_API_KEY;
$this->smsSenderId = self::DEF_SMS_SENDER_ID;
public function send_twofactor_sms(string $phonenumber): mixed
{
$securepin = mt_rand(10000,99999);
$curl = curl_init();
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Bearer " . $this->smsApiKey
);
$params = array(
'to' => $phonenumber,
"bypass_optout" => true,
'message' => "Your verification PIN is \n" . $securepin,
'sender_id' => $this->smsSenderId
);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://" . $this->smsApiHost,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FAILONERROR => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($params),
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($curl);
if (curl_errno($curl)) {
throw curl_error($curl);
}
curl_close($curl);
return $response;
}
Checking and performing next step
$checkauth = json_decode($response, true);
if ($checkauth[0]["success"] == true) {
// PIN Verified - Do some SQL/friendly message, etc
}
Response:
{
"message": "Message is queued for sending! Please check report for update",
"success": true,
"message_id": "e7745289-7236-497f-acf2-f9cfd6a86f16"
}
Keep in mind, $securepin is not set because you can do it yourself - this is just to get you started. I use my own way.
Last edited by STorrents2020 on 2022-02-10 08:59:35
