Skip to content

Instantly share code, notes, and snippets.

@jaboutboul
Created November 15, 2011 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaboutboul/8bb0f321ce0a253f8517 to your computer and use it in GitHub Desktop.
Save jaboutboul/8bb0f321ce0a253f8517 to your computer and use it in GitHub Desktop.
Twilio Example: Anonymously Connecting Two Callers
<?php
//Include the Twilio PHP Helper Library
include "Services/Twilio.php";
//Set the variables for out accountSID and authToken
$account_sid = "";
$auth_token = "";
//Set our outgoing caller id as our Twilio number
$your_twilio_number = "";
//Assign the phone number of the first participant
$participant1 = "";
//Define a new object to interact with the API. Give it out Account SID and Auth Token
$client = new Services_Twilio($account_sid, $auth_token);
//Make the call to the first participant using the Twilio API. Once answered, the call flow will go to the url we are passing in
$call = $client->account->calls->create(
$your_twilio_number,
$participant1,
'http://example.com/secondleg.php'
);
?>
<?php
//Set our header so that we return TwiML
header("Content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
//Set our outgoing caller id as our Twilio number
$your_twilio_number = "";
//Assign the phone number of the second participant
$participant2 = "";
//Below we return our TwiML telling our call flow to now dial the second participant, connecting both participants.
?>
<Response>
<Dial callerId="<?php echo "$your_twilio_number";?>"><Number>
<?php echo "$participant2";?>
</Number></Dial>
</Response>
@jaboutboul
Copy link
Author

There are two files here, firstleg.php and second leg.php. They way they work is as follows:

  • In firstleg.php we first define a few necessary items, such as our Twilio account SID and authentication token (necessary to interact with the Twilio API), our Twilio number (the anonymous middleman number which we want both parties to see) and we also specify the phone number of the first participant. Next, we use the Twilio PHP helper library to make a request to the Twilio API telling it to place a call from our Twilio number to the first participant and then give control of the flow over to the 'secondleg.php' file once the first participant answers the call.
  • In secondleg.php we again define our Twilio number and then define the phone number of participant number 2. We then tell Twilio using TwiML, which is Twilio's XML-based markup language for call flow control to place a call from our Twilio number to the second participant.

The first participant will hear the call connecting and once the second picks up, they will be able to speak to each other, each seeing our Twilio number as the caller ID and not the other party.

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