Are you facing an increase in attempts by spam to gain unauthorized access to your website? Once you’ve launched your website and you haven’t put any spam prevention measure in place. Verification Code allows you to protect your website by adding send SMS verification functionality to User Registration functions before the user can see the full content of your website.
Verification Code is used Twilio Networks HTTP API to send messages, meaning you will need a valid Twilio Networks username and password in order to be able to send SMS messages. The account can be obtained from the https://www.twilio.com.
Basic Php Integration to Send SMS with Twilio
1. How to Setup Twilio
To send SMS and outgoing messages perform an HTTP POST to the Messages resource URI. We will also use the Twilio PHP Library for making REST requests.
If you are using a Twilio Trial account for this example, you will only be able to send SMS messages to phone numbers that you have verified with Twilio. Phone numbers can be verified via your Twilio Console’s Verified Caller IDs. The ‘From’ parameter will also need to be a phone number you purchased from Twilio.
Create an account on Twilio.
- Follow this link for creating account on Twilio https://www.twilio.com.
- If you created the trial account on Twilio then you can send SMS to only the verified number of Twilio account.
- For send SMS on any number you need to create a premium account or upgrade the Twilio account.
2. Scheduled SMS campaigns
You’ll be assigned a number when you sign up to Twilio that you can use for testing but can’t send any actual texts through it. We just credited the account straight away so we could see the text coming through life. For us and probably most others we only needed to send a handful of texts whilst testing. As you’ll see further down this write-up the library provided by Twilio is super simple and you are only charged when a text is successfully delivered so for the cost of having the convenience of actual messages being delivered to real phones was well worth it.
Sign up for a free Twilio account and buy a phone number.
3. Grab the PHP library and your API credentials
Download the PHP library provided by Twilio and put it somewhere that it can be included in the PHP script you want to use to send the SMS text. When sending your messages you’ll also need your account sid and your Auth Token. These can be grabbed from your account settings.
4. Set up the sending of the message
Firstly include the PHP library you’ve just downloaded and unzipped:
require "PATH YOUR TWILIO LIBRARY/Services/Twilio.php"; $client = new Services_Twilio('ACCOUNT SID HERE', 'AUTHTOKENHERE');
You have to find your account SID and auth token in your console dashboard, and your phone number in your phone numbers list. Create a file called settings.php to store these values.
$account_sid = "YOURACCOUNTSID"; $auth_token = "YOURAUTHTOKEN"; $twilio_phone_number = "+YOURTWILIONUMBER";
Create a file called send-SMS.php and paste the code below to:
- Include the helper library and settings file
- Create a Twilio client using your account credentials
- Create a new message from our Twilio number to your cellphone
require "PATH YOUR TWILIO LIBRARY/Services/Twilio.php"; include 'settings.php'; $client = new Services_Twilio('ACCOUNT SID HERE', 'AUTHTOKENHERE'); $client->account->messages->create(array( 'To' => 'NUMBER OF RECIPIENT', 'From' => 'NUMBER PURCHASED WITH YOUR ACCOUNT', 'Body' => "THIS IS YOUR TEXT MESSAGE", ));
Save your file and run it:
It’s as easy as that! You’ll now be sending live text messages via PHP and Twilio. You can keep an eye on the outgoing messages via the logs area of your Twilio account area and keep an eye on costs via the usage area.
Following on from that we set the header type text/plain and simply output what we’d like to be text back to the number that has sent the text, Twilio does the rest. That’s sending text messages and handling a response via a text all wrapped with a small amount of code.