Last active
August 14, 2022 03:24
-
-
Save jmadden/7fa317ea2e6bb782803b7b8876cf1836 to your computer and use it in GitHub Desktop.
Twilio PHP Outbound Call and Gather Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add the URL to Twilio incoming webhook.