Posts

Ultimate Guide to send OTP using PHP(Twilio) in 5 minutes?

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 create account on Twilio https://www.twilio.com.
  • If you created 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 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 live. 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 the 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 in the code below to:

  • Include the helper library and settings file
  • Create a Twilio client using our account credentials
  • Create a new message from our twilio number, to our 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:
php send-sms.php

Conculsion

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 a text messages and handling a response via a text all wrapped with small amount of code.

References

Twilio Docs
The Twilio Php Helper Library
SEND SMS AND MMS MESSAGES IN PHP

Further Reading

Twilio voice: Receive Call On Your Android Mobile
Easy way to play an audio file on call in android using twilio
How to enable two step verification in Socialengine websites

Performance optimization tips for SocialEngine and Phpfox in 2018

Are you fed up with performance issues on your website? Try these performance optimization tips right into the hosting to give the boost to social communities. Yahoo reported a 9% increase in traffic for every 400-millisecond increase in performance. Walmart experienced a 2% increase in conversions for every 1-second improvement in performance.  Amazon experienced a 1% increase in revenue for every 100 ms increase in performance. Google, the behemoth as it is in providing you with visibility, also ranks your website according to its speed. What does this tell us?  The faster your community is, the faster it will grow!

Now when you run a social networking platform or any other framework for that matter. It would run perfectly fine when the number of users is less. As you would grow your website becomes sluggish. At this point, each platform requires performance optimisation. There are several techniques to do this. There are many variety of tools – choosing which ones are going to work wonders for you, is tricky. Being around in this area for several years, we have used and continue to study and experiment with various approaches. Following are some of the techniques that have worked for a significant percentage of our Social Engine/Phpfox customers.

PHP Performance Optimization

PHP Performance Optimization

  • Realpath PHP cache settings are increased
  • PHP open_basedir is disabled for increased PHP performance
  • PHP-FPM (FastCGI Process Manager) is an alternative high-performance method of running PHP based web applications
  • Alternative PHP Cache (APC) is enabled and optimized for SocialEngine.

Web Server Performance Optimization

  • Keep-Alives are enabled with suitable values based on traffic from Google Analytics
  • Gzip output compression is enabled
  • Installing plugins for javascript and CSS compression
  • Using reverse proxy for caching static resources

Database Performance Optimization

  • Alternative engines of MySQL for better Socialegine performance
  • Database configuration based on what works best for SocialEngine
  • Scripts for optimizing indexes of tables most used

Custom Optimization

  • Profiling database queries and improves the most used ones
  • Improvements in plugins that may be slowing your site down or using alternative ones

It is possible to do all of this by yourself or getting developers to do this. However, in our experience, the community owners are really more focused on making their community an engaging experience via functionality or graphics. Similarly, most developers can write PHP code, but very few have the experience of various tools for improving performance. One of the solutions we thought was to bake this optimization right into the hosting. So we have teamed up with Acenet hosting to provide you hosting for SocialEngine and Phpfox. Which associated with built-in PHP, web server and database performance enhancements. Custom optimization may be required when your community grows huge, but that is different for everyone. We will continue to provide it as a separate service.

Please feel free to add your choice of recommendations and tools you feel have re-powered your social community performance in the comments below. We would certainly love to add them into the list for our readers.

References

Further Readings