Create a Twilio Voice Pricing Calculator using PHP
Time to read: 3 minutes
A major concern when building web solutions with third-party services is estimating the costs of API requests. This tutorial will guide you through the process of finding the costs of voice calls for Twilio Programmable Voice, depending on the origin and destination.
Installation Requirements
Getting started with this tutorial, it is expected that you have the following modules/packages installed on your development machine:
Here is a guide that can be followed to install Composer globally:
Creating the PHP project
Let’s create a folder named pricing
on your local machine where the code will be housed.
Inside this folder, create an index.php
file which will contain all the code for this tutorial.
Next, proceed to install Twilio's PHP SDK to call the necessary APIs needed for the functions to be shared. To achieve this, use Composer to install the dependency with the following command:
Because we wouldn’t want private credentials such as your Twilio keys available to the public, you will create a .env
file to store your environment variables. The .env
file will be read using the popular PHP package vlucas/phpdotenv to store those variables.
To install the package, use the command below:
Once the installation is complete, make sure to set the Twilio API credentials in your .env
file, as they will be used for authenticating the email requests. Add the following block to the file:
NOTE: Your Twilio credentials can be retrieved from the Twilio console. Your phone number can be purchased from the Active Numbers dashboard.
Next, create your index.php
file and include the Composer autoloader, and the Twilio PHP SDK like so:
After doing that, we will initialize the Twilio client so we can have a connection to the Twilio API using the Account SID and Auth Token. Add the following code:
Creating the View
It is important to have a visual interface where all code can be tested and viewed. For the sake of this guide, we will be calling it index.php
. Add the following code to the created view, below the PHP code:
Fetching Number Information
The Twilio Programmable Voice API allows you to retrieve the prices required to make voice calls to phone numbers in specified countries and locations regardless of the origin country of the caller. To be able to access this information, add this code block below the existing PHP code:
Add the code below to determine the cost of the call from your Twilio number to the destination number:
Full Code
This is the complete code for this tutorial. If you haven’t already, copy and replace all of the code within your index.php
file with the code below:
Testing
To see the application in action, start up your development server using this command in the terminal:
Finally, using the web browser of your choice, visit http://localhost:8000. Fill in the forms and submit them to get the expected response.
Conclusion
Upon completing all of the enlisted steps, you can easily and successfully retrieve account-specific pricing for voice calls from different locations, and various telecommunication companies available in those locations, and lastly, the price for Twilio phone numbers.
This tutorial can also be extended to retrieve the call cost for various numbers on the go, and even create a trigger to either initiate calls (if the costs fall within your budget), or send out SMS and emails.
I will be delighted to see what you build and if for any reason you need my help with a task related to this tutorial, I will be glad to be of assistance. You can reach me through any of the channels provided below.
Ugendu Martins Ositadinma
- Email: ugendu04@gmail.com
- Twitter: https://twitter.com/ohssie_
- Github: https://github.com/ohssie
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.