Skip to content

Instantly share code, notes, and snippets.

@jmadden
Last active August 14, 2022 03:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jmadden/7fa317ea2e6bb782803b7b8876cf1836 to your computer and use it in GitHub Desktop.
Save jmadden/7fa317ea2e6bb782803b7b8876cf1836 to your computer and use it in GitHub Desktop.
Twilio PHP Outbound Call and Gather Example
<?php
// Get the PHP helper library from https://twilio.com/docs/libraries/php
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "YOU_ACCOUNT_SID";
$token = "YOUR_AUTH_TOKEN";
$client = new Client($sid, $token);
$to = "+14155551212";
$from = "+14158675310";
$call = $client->calls->create(
$to,
$from,
array(
"url" => "/2-call-answered-gather.php"
)
);
<?php
// Create a route that will handle Twilio webhook requests, sent as an
// HTTP POST to /voice in our application
require_once '/path/to/vendor/autoload.php';
use Twilio\Twiml;
// Use the Twilio PHP SDK to build an XML response
$response = new Twiml();
// Use the <Gather> verb to collect user input
$gather = $response->gather(array('numDigits' => 1, 'action' => '/3-gather.php'));
// use the <Say> verb to request input from the user
$gather->say('Press 1 to take a survey. Press 2 to hear a joke.');
// If the user doesn't enter input, loop
$response->redirect('/2-call-answered.php');
// Render the response as XML in reply to the webhook request
header('Content-Type: text/xml');
echo $response;
<?php
// Create a route that will handle Twilio Gather verb action requests,
// sent as an HTTP POST to /gather in our application
require_once '/path/to/vendor/autoload.php';
use Twilio\Twiml;
// Use the Twilio PHP SDK to build an XML response
$response = new Twiml();
// If the user entered digits, process their request
if (array_key_exists('Digits', $_POST)) {
switch ($_POST['Digits']) {
case 1:
$response->say('You selected to take the survey. Thank you!');
break;
case 2:
$response->say('You selected to her a joke! Why did the robot cross the road? Because it was carbon bonded to the chicken!');
break;
default:
$response->say('Sorry, I don\'t understand that choice.');
$response->redirect('/2-call-answered.php');
}
} else {
// If no input was sent, redirect to the /voice route
$response->redirect('/2-call-answered.php');
}
// Render the response as XML in reply to the webhook request
header('Content-Type: text/xml');
echo $response;
@jmadden
Copy link
Author

jmadden commented Jan 12, 2018

Note: require_once '/path/to/vendor/autoload.php'; is referencing the Twilio PHP helper library you can find here https://github.com/twilio/twilio-php or install using Composer. composer require twilio/sdk

Either way, you need to reference the Twilio PHP helper library in order for this code to work.

Essentially the way the code above works is:

  1. 1-outbound-call.php makes an outbound call to a number.
  2. The number answers and TwiML generated at 2-call-answered.php is run, asking the called party to make a selection.
  3. The person called makes a selection and that selection is sent to 3-gather.php, where more TwiML is generated and interacts with the called party.

@alexsdesign
Copy link

Just want to say a massive thank you.
Finding your code has helped me massively to get started with gathering DTMF and outbound calls :D

@jmadden
Copy link
Author

jmadden commented May 2, 2022

Happy to hear it was helpful 👍 @alexsdesign

@haider11234
Copy link

PHP Fatal error: Uncaught Twilio\Exceptions\RestException: [HTTP 400] Unable to create record: Url is not a valid URL: /2-call-answered.php in D:\Progamming\project\p38\vendor\twilio\sdk\src\Twilio\Version.php:88
Stack trace:
#0 D:\Progamming\project\p38\vendor\twilio\sdk\src\Twilio\Version.php(223): Twilio\Version->exception()
#1 D:\Progamming\project\p38\vendor\twilio\sdk\src\Twilio\Rest\Api\V2010\Account\CallList.php(94): Twilio\Version->create()
#2 D:\Progamming\project\p38\1-outbound-call.php(18): Twilio\Rest\Api\V2010\Account\CallList->create()
#3 {main}

@mprince2k18
Copy link

PHP Fatal error: Uncaught Twilio\Exceptions\RestException: [HTTP 400] Unable to create record: Url is not a valid URL: /2-call-answered.php in D:\Progamming\project\p38\vendor\twilio\sdk\src\Twilio\Version.php:88 Stack trace: #0 D:\Progamming\project\p38\vendor\twilio\sdk\src\Twilio\Version.php(223): Twilio\Version->exception() #1 D:\Progamming\project\p38\vendor\twilio\sdk\src\Twilio\Rest\Api\V2010\Account\CallList.php(94): Twilio\Version->create() #2 D:\Progamming\project\p38\1-outbound-call.php(18): Twilio\Rest\Api\V2010\Account\CallList->create() #3 {main}

Add the URL to Twilio incoming webhook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment