Calculating Script Execution Time in PHP to Send an SMS with Twilio
Time to read: 3 minutes
New PHP developers may not be aware that every script you run has 30 seconds to complete. If your API requests, database transactions, and data processing don’t complete in time, you will experience a server timeout. This is because PHP's max_execution_time helps prevent poorly written scripts from tying up the server.
Most developers will spend time Googling workarounds instead of focusing on the fact that this behavior is not a bug. In reality, people shouldn’t wait a whole minute or more for your page to load or process to complete within the browser. It’s just not an ideal user experience.
In this tutorial, we’ll learn how to measure the time it takes to send an SMS with Twilio Programmable Messaging for better application performance. At the conclusion, you will have learned how to:
- Build a simple script to send an SMS in PHP
- Use PHP’s microtime() function to measure script execution time
- Make more performant decisions with your application
Get Started
We’ll only be writing a short script for this tutorial. As a result, you’ll only need:
- A Twilio account (If you don’t have one, here’s $10 to get started). If you have already set up your account, login to grab your Account SID and Auth Token and copy them to a secure location on your computer.
- Composer (the PHP dependency manager), globally installed.
At this point the assumption is being made that you already have PHP installed locally.
Create a new PHP script called script-time.php in the location of your choosing. If you’re on a linux-based machine, you can use the following command:
Add Composer to the project and initialize a new composer.json file which will allow us to add the Twilio SDK to this project.
Add the Twilio SDK for PHP to the project:
Now open script-time.php in your favorite code editor (or IDE) and require
it at the top of the file:
This will define the Twilio Rest Client and make it available for use to send an SMS. Up until this point, this is the standard process for adding the Twilio SDK to any PHP project.
Define Your Twilio Credentials
Your Twilio credentials will be required to actually connect to the Twilio API. To get started, define three new variables $account_sid
, $auth_token
, and $twilio_number
to hold your Account SID, Auth Token, and Twilio Phone Number respectively. If you need to purchase a number, you can do so from the Twilio console.
Add the following code underneath the previous declaration:
NOTE: Your Account SID should never be used in a production environment.
This script is almost ready to send a text message. Create a new $client
variable to hold a newly initialized Twilio Rest Client, authenticated with your credentials.
Send an SMS
A variable $start_time
will be added to record the start of the process that sends an SMS. It utilizes the built-in PHP microtime()
function to record the start time.
Next, add the following code to send a sample text message:
After replacing “your phone number “with your actual phone number to receive the message, this code is fully functional to transmit and SMS.
Now record when the API call is completed, calculate the time difference, and display the results:
Measure the Script Execution time
It’s time to test your script by running the script in your command line:
I would recommend running this and recording the results at least three times.
How long did it take to send? For me it was an average of .33 seconds.
Remember, the max execution time of a PHP script is 30 seconds. Depending on your hosting environment, you may or may not be able to change this value for your application. Also, considering what's best for performance, you honestly shouldn't leave the person using your app waiting for longer than 30 seconds for a script to process within their browser.
Let's do some math.
(30 seconds / .33 seconds per message) = 90 API calls before the default timeout. This means that if you had an array with 100 numbers, your script would time out before sending.
Conclusion
With this knowledge in hand, you can now make more informed decisions about building your communications-based app. Maybe you need to refactor your code? Maybe the script should be asynchronously called from the front end with AJAX requests. Either way, you can now build more intentionally and efficiently.
In a subsequent post, I will show you how to process 1000 or more messages within this 30 second window using Twilio Notify.
Marcus Battle is a Senior Manager of Developer Voices at Twilio where he prompts and rallies developers to build the future of communications. He can be reached via:
- Email: mbattle@twilio.com
- Twitter: @themarcusbattle
- Github: https://github.com/themarcusbattle
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.