Skip to content

Instantly share code, notes, and snippets.

@jfinstrom
Last active October 7, 2015 13:55
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 jfinstrom/439bf68d64111ba75ffc to your computer and use it in GitHub Desktop.
Save jfinstrom/439bf68d64111ba75ffc to your computer and use it in GitHub Desktop.
Last 30 days of CDR as CSV with FreePBX bootstrap
#!/usr/bin/env php
<?php
if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
include_once('/etc/asterisk/freepbx.conf');
}
$outfile = '/tmp/report.csv';
$dbh = \FreePBX::Database();
$sql = "SELECT * FROM asteriskcdrdb.cdr WHERE date(calldate) >= date(now()-interval 30 day);";
$stmt = $dbh->prepare($sql);
$stmt->execute();
$ret = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$colnames = array_keys($ret[0]);
$csv = fopen($outfile,'w+');
fputcsv($csv,$colnames);
foreach($ret as $row){
fputcsv($csv,$row);
}
fclose($csv);
@lgaetz
Copy link

lgaetz commented Oct 7, 2015

Requires FreePBX 12 or higher

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