Skip to content

Instantly share code, notes, and snippets.

@TMcManus
Last active August 18, 2024 22:49
Show Gist options
  • Save TMcManus/4004512 to your computer and use it in GitHub Desktop.
Save TMcManus/4004512 to your computer and use it in GitHub Desktop.
Twilio Call Log CSV Download Script
<?php
/**
* Download the library from: https://github.com/twilio/twilio-php
* Copy the 'Services' folder into a directory containing this file.
*/
require('Services/Twilio.php');
$account_sid = "ACXXXXXXXXXXX"; // Your Twilio account sid
$auth_token = "YYYYYYYYYYYY"; // Your Twilio auth token
// Download data from Twilio API
$client = new Services_Twilio($account_sid, $auth_token);
$calls = $client->account->calls->getIterator(0, 1000, array(
'StartTime>' => '2012-11-19',
'StartTime<' => '2012-11-26',
//'Status' => 'busy', // **Optional** filter by Status...
//'From' => '+17075551234', // ...by 'From'...
//'To' => '+18085559876', // ...or by 'To'.
));
// Browser magic
$filename = $account_sid."_calls.csv";
header("Content-Type: application/csv") ;
header("Content-Disposition: attachment; filename={$filename}");
// Write headers
$fields = array(
'Call SID', 'From', 'To', 'Start Time', 'End Time',
'Status', 'Direction', 'Price',
);
echo '"'.implode('","', $fields).'"'."\n";
// Write rows
foreach ($calls as $call) {
$row = array(
$call->sid, $call->from, $call->to, $call->start_time, $call->end_time,
$call->status, $call->direction, $call->price,
);
echo '"'.implode('","', $row).'"'."\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment