Retrieving Account-Specific Pricing for Twilio SMS in Your PHP App
Time to read: 2 minutes
A major factor to consider when building web applications is evaluating the cost of integrating third-party services. If you’re using Twilio, this tutorial will serve as a guide to finding the cost of making SMS/Messaging requests to the Twilio Programmable SMS APIs.
Installation Requirements
To get started with this tutorial, you will need to 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
First, create a folder named smsPricing
on your local machine where the code will be housed.
In the folder that was created, create a file named index.php which will contain all the code for this tutorial.
Now, let’s go ahead and install Twilio's PHP SDK in order to call the necessary APIs required to share the functions. To accomplish this, use Composer to install the dependency with the command provided below:
We don’t want your private credentials such as your Twilio keys available to the public, so 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:
As soon as the installation is complete, set the Twilio API credentials in your .env file, as you will need them to authenticate the API requests. Add the following block below to the file:
NOTE: Your Twilio credentials can be retrieved from the Twilio console.
Next, create your index.php
file to 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
We will need to have an interface to be able to easily communicate with the code. Add the following code to index.php
, below the PHP code:
NOTE: Be sure to close your PHP code with `?>` before the HTML above.
Retrieving Messaging Prices
This block can be used to find the cost of sending and receiving SMS to phone numbers of different countries depending on your country of choice:
Complete Code
Here is the complete code for this tutorial. It is essential that you copy and replace all of the code within your index.php
file with the code below:
Testing
To ensure that your code is defect-free and 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 form and submit to get the expected response.
Conclusion
Upon completing the steps above, you can easily retrieve account-specific messaging prices for various countries and various telecommunication companies available in those locations.
This tutorial can also be extended to compare messaging prices for various telecommunication companies in the destination country/location.
I am looking forward to seeing what you build. In case you need help or want to say “Hi”, 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.