Phone Calls

Making a Phone Call

The Calls resource allows you to make outgoing calls:

$client = new Services_Twilio('AC123', '123');
$call = $client->account->calls->create(
  '9991231234', // From this number
  '8881231234', // Call this number
  'http://foo.com/call.xml'
);
print $call->length;
print $call->sid;

Adding Extra Call Parameters

Add extra parameters, like a StatusCallback when the call ends, like this:

$client = new Services_Twilio('AC123', '123');
$call = $client->account->calls->create(
    '9991231234', // From this number
    '8881231234', // Call this number
    'http://foo.com/call.xml',
    array(
    'StatusCallback' => 'http://foo.com/callback',
    'StatusCallbackMethod' => 'GET'
    )
);

A full list of extra parameters can be found here.

Listing Calls

It’s easy to iterate over your list of calls.

$client = new Services_Twilio('AC123', '123');
foreach ($client->account->calls as $call) {
    echo "From: {$call->from}\nTo: {$call->to}\nSid: {$call->sid}\n\n";
}

Filtering Calls

Let’s say you want to find all of the calls that have been sent from a particular number. You can do so by constructing an iterator explicitly:

$client = new Services_Twilio('AC123', '123');
foreach ($client->account->calls->getIterator(0, 50, array(
    'From' => '+14105551234'
)) as $call) {
    echo "From: {$call->from}\nTo: {$call->to}\nSid: {$call->sid}\n\n";
}

Accessing Resources from a Specific Call

The Call resource has some subresources you can access, such as notifications, recordings and feedback. If you have already have a Call resource, they are easy to get:

$client = new Services_Twilio('AC123', '123');
foreach ($client->account->calls as $call) {
  $notifications = $call->notifications;
  if (is_array($notifications)) {
    foreach ($notifications as $notification) {
      print $notification->sid;
    }
  }

  $transcriptions = $call->transcriptions;
  if (is_array($transcriptions)) {
    foreach ($transcriptions as $transcription) {
      print $transcription->sid;
    }
  }

  $recordings = $call->recordings;
  if (is_array($recordings)) {
    foreach ($recordings as $recording) {
      print $recording->sid;
    }
  }

  $feedback = $call->feedback;
  if ($feedback !== null) {
    print $feedback->quality_score
  }
}

Be careful, as the above code makes quite a few HTTP requests and may display PHP warnings for uninitialized variables.

Retrieve a Call Record

If you already have a Call sid, you can use the client to retrieve that record.:

$client = new Services_Twilio('AC123', '123');
$sid = "CA12341234";
$call = $client->account->calls->get($sid)

Delete a Call Record

To protect your users’ privacy and/or comply with legal requirements, Twilio allows you to delete call records:

$client = new Services_Twilio('AC123', '123');
$sid = "CA12341234";
$call = $client->account->calls->get($sid);
$call->delete();

Modifying live calls

The Call resource makes it easy to find current live calls and redirect them as necessary:

$client = new Services_Twilio('AC123', '123');
$calls = $client->account->calls->getIterator(0, 50, array('Status' => 'in-progress'));
foreach ($calls as $call) {
  $call->update(array('Url' => 'http://foo.com/new.xml', 'Method' => 'POST'));
}

Ending all live calls is also possible:

$client = new Services_Twilio('AC123', '123');
$calls = $client->account->calls->getIterator(0, 50, array('Status' => 'in-progress'));
foreach ($calls as $call) {
  $call->hangup();
}

Note that hangup will also cancel calls currently queued.