Chris Chris - 9 months ago 82
PHP Question

Pass dynamic TwiML when making calls

How can I pass dynamic TwiML to the Twilio API when making calls?

$client = new Services_Twilio($sid, $token);

So instead of passing a URL to fetch the TwiML:

$call = $client->account->calls->create("+14158675309", "+14155551212", "", array());

Could I dynamically generate the TwiML and pass it to the API?

$twiml = new Services_Twilio_Twiml();
$twiml->say( 'Hello Mark');
$call = $client->account->calls->create("+14158675309", "+14155551212", $twiml);


Twilio evangelist here.

Instead of specifying a static XML file in the create function, you can make this a PHP file and dynamically generate the response.

You still use create() to tell Twilio to initiate the phone call. When the call is answered, Twilio will request the URL you've specified to get the TwiML that tells it how to proceed with the call. So for example you change:


And have the PHP generate the TwiML output:

$twiml = new Services_Twilio_Twiml();
$twiml->say( 'Hello Mark');

Here is the documentation for generating TwiML using the PHP helper library:

Hope that helps.