As you might have noticed from many of my previous posts, I am a big fan of Twilio. The reason is because it is really easy to use, its very reliable, and it is also cheap. There are a ton of cool things you can do with Twilio, from Sending SMS to Receiving and Dialing calls. This simple tutorial will show you how easy it is to use Twilio to place outbound calls.
Placing outbound calls using Twilio
There are two ways you can dial calls with Twilio. One way is using their Rest API. The other way is using TwiML. With TwiML you don’t have to rely on the Rest API at all. You don’t need to include SDKs, you can do this in a few lines of code in a single file. The only catch to this is that you can only use this method (TwiML) with calls that are already in progress, you cannot initiate a new call leg with TwiML. As opposed to the Rest API, which allow you to initiate call legs. TwiML are meant to control the flow of a call that has already been initiated without having to make requests to the Rest API which could take long sometimes, affecting the overall efficiency of your application.
Start dialing out
Lets go ahead and create a directory where we will be placing the code to dial outbound calls “/twilio-voice-demo”. The next step is to include the Twilio SDK in this directory so we can make requests to the Twilio Rest API.
Please note that for this example we will be using PHP, but this can also be done any many other lnaguages such as Ruby, .Net and Node js
Go ahead and download the Twilio PHP SDK from Github and place only the directory named “Services” inside the directory we just created “/twilio-voice-demo”.
So far this directory should contains only the Twilio PHP SDK, so lets go ahead and create our own PHP file where we will be writing our code. Create a new file and call it “twilio-voice-demo.php”. The first thing we need to do in this file is require the Twilio Rest API.
1 2 3
<?php require('Services/Twilio.php'); ?>
Let’s add the code to initiate the outbound call now.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php require('Services/Twilio.php'); // Your Twilio credentials $sid = "YOUR_ACCOUNT_SID"; $token = "YOUR_AUTH_TOKEN"; $client = new Services_Twilio($sid, $token); $to_number = "+14158675309"; $from_number = "+14155551212"; /** * A url that wil be requested when the call gets connected * This url must contain an XML - (TwiML) **/ $connect_url = "http://webtricksandtreats.com/twilio-voice-demo.xml"; $additional_parameters = Array(); $call = $client->account->calls->create($to_number, $from_number, $connect_url, $additional_parameters); //outputs the call sid echo $call->sid; ?>
All you need to make this work are three parameters $to_number, $from_number and $connect_url. The $additional_parameters are not required, but you could use this to pass an array with specifications on how to treat this call. If you run this file now in your computer using MAMP or XAMP this file will place a call to the number specified, and the caller ID will be the $from_number. Note that for the caller ID you cannot just use any number. This number has to be verified by Twilio before you can use it as called ID. This is to prevent spoof calls. Verify a number with Twilio
And that is how you place outbound calls from your computer. This is very basic, if you want to learn what to do after the call is connected, you should read Manipulating calls with Twilio using TwiML. Thanks again for visiting, please remember to comment bellow any questions or suggestions you might have.
Latest posts by Enmanuel Corvo (see all)
- How to combine multiple Jira Boards in a single page view - August 27, 2017
- Twilio Signal 2016 - May 26, 2016
- Finding great WordPress Themes & Plugins - December 8, 2015