Tutorial: Outbound Phone Calls with Twilio via PHP

0 Comments 14 January 2015

We are going to do a simple tutorial to get you started on how to make outbound calls via Twilio. We will use PHP because it is the easiest for a program to pick up. If you are unaware of what PHP is, it is a server-side scripting language. You can read about it all over the web.

Twilio is a wonderful application to keep in touch with customers or write unique web applications. Below is simple code that sends a call to a phone number. Take a look at it and we will explain part by part:

 

 

<?php
	/* Include the Twilio PHP library */
	require "twilio.php";

	/* Twilio REST API version */
	$ApiVersion = "2010-04-01";

	/* Set our AccountSid and AuthToken */
	$AccountSid = "TWILIO ACCOUNT ID";
	$AuthToken = "TWILIO ACCOUNT PASSWORD";

	// Outgoing Caller ID you have previously validated with Twilio 
	$CallerID = 'PLACE CALLER ID OF YOUR NUMBER';

	/* Instantiate a new Twilio Rest Client */
	$client = new TwilioRestClient($AccountSid, $AuthToken);

	/* Initiate a new outbound call by POST'ing to the Calls resource */
	$response = $client->request("/$ApiVersion/Accounts/$AccountSid/Calls", 
		"POST", array(
		"From" => $CallerID,
		"To" => "PLACE NUMBER YOU WISH TO CALL",
		"Url" => "http://demo.twilio.com/welcome/voice/"

	));
	if($response->IsError)
		echo "Error: {$response->ErrorMessage}";
	else
		echo "Started call: {$response->ResponseXml->Call->Sid}";
?>

Explanation

Step 1:
Before the code can begin, you must include the Twilio Library. You can download the library here.

<?php
	/* Include the Twilio PHP library */
	require "twilio.php";

All this snipped does is require the library and add it in. Simple as that.

Step 2:

This next part is critical. The first variable $ApiVersion stats the latest available API. This does not need to be changed ever.

<?php
	/* Twilio REST API version */
	$ApiVersion = "2010-04-01";

	/* Set our AccountSid and AuthToken */
	$AccountSid = "TWILIO ACCOUNT ID";
	$AuthToken = "TWILIO ACCOUNT PASSWORD";

The second part is where you will add your Twillio account information retrieved from your account are at Twilio. These are secret ID’s that are unique to a specific Twilio account. Do not share these with anyone.

Step 3:

// Outgoing Caller ID you have previously validated with Twilio 
	$CallerID = 'PLACE CALLER ID OF YOUR NUMBER';

If you would like to show your Caller ID then add that number in the variable. To add a Caller ID, you must authenticate it in your Twilio account first. Do that by logging in and following the instructions in your account.

Step 4:
The snipped of code below is critical as well. In Step 1 and Step 2, you included a library and include essential code for your Twilio Account. What TwilioRestClient does is create a instance of the Twilio API with your credentials.

	/* Instantiate a new Twilio Rest Client */
	$client = new TwilioRestClient($AccountSid, $AuthToken);

Step 5:

This is a POST response via a PHP form. This is the information that is transmitted back to Twilio so they can initiate the call. The only thing that needs to be changed here is the “TO” section. Place your phone number you would like to call here.

	/* Initiate a new outbound call by POST'ing to the Calls resource */
	$response = $client->request("/$ApiVersion/Accounts/$AccountSid/Calls", 
		"POST", array(
		"From" => $CallerID,
		"To" => "PLACE NUMBER YOU WISH TO CALL",
		"Url" => "http://demo.twilio.com/welcome/voice/"

	));

Step 6:

	if($response->IsError)
		echo "Error: {$response->ErrorMessage}";
	else
		echo "Started call: {$response->ResponseXml->Call->Sid}";

Once POST request is sent to Twilio, they will either respond with an error stating that the information send was wrong or they will send you the success code. Congrats! You have successfully made a call!

Conclusion

Twilio is a wonderful way for developers to integrate Voice and SMS support to their applications. Stay tuned for the next tutorial on SMS code and explanation. If you are a developer and would like to incorporate business VoIP then this is a wonderful way to achieve that. Stay tuned for more!

Originally posted 2011-07-24 22:58:59.

More VoIP information
  1. Top 3 Innovative VoIP Services of 2011
  2. Keku with Google Voice gives International Call Forwarding!
  3. ConferenceBuster Free VOIP Conference Calling Service
  4. Exclusive: Rebtel Coupon Code for 30 Minutes of Free Calls
  5. 8×8 New Facebook App with Free US Calls

Categorized under API, Telecom

Share your thoughts

© 2017 CheapestVoIPCalls.net.