dslreports logo
site
 
    All Forums Hot Topics Gallery
spc

spacer




how-to block ads


Search Topic:
uniqs
1093
share rss forum feed

dani

join:2012-03-20
canada

My CDR & account info web script

Click for full size
So I started with the samples provided, added some snippets I found and basic formatting and ended up with something useful (at least for me).

To run it you just need the api enabled, class.voipms.php in the same folder with the api credentials at the top, and edit the acct nr in the script.

A couple of things are commented out for various reasons but work fine.
It can be further hacked in lots of different ways.

<?php
    session_start();
require_once("class.voipms.php");
$voipms = new VoIPms();
 
/* Account */
$account = "111111";
 
/*
// Get Registration Status 
$response = $voipms->getRegistrationStatus($account);
 
// Get Errors - Invalid_Account 
if($response[status]!='success'){
    echo $response[status];
    exit;
}
 
// Is Registered 
echo "{$account} Registered : {$response[registered]}";
*/
 
$response1 = $voipms->getBalance($account);
 
$today = date("Y-m-d");
$yesterday = date("Y-m-d", strtotime("-2 days"));
 
/* Get CDR */
$response = $voipms->getCDR($yesterday,$today,1,1,1,1,-5,'all','all','all');
 
/* Get Errors - no_cdr */
if($response[status]!='success'){
    echo $response[status];
    exit;
}
 
/* Get CDR Array */
$cdr = $response[cdr];
 
/* Calls */
$calls = count($cdr);
 
/* Duration / Total */
foreach($cdr as $call){
    $duration += $call[seconds];
    $total += $call[total];
}
$duration = secToTime($duration);
 
?>
<table cellspacing="2" cellpadding="6" style="border: 1px solid #336699;font-size: 12px;font-family:Arial">
 
    <tr style="background-color: #D7F3EC">
        <td style="border: 1px solid #336699">Balance</td>
        <td style="border: 1px solid #336699">Total Spent</td>
        <td style="border: 1px solid #336699">Total Calls</td>
        <td style="border: 1px solid #336699">Total Time</td>
        <td style="border: 1px solid #336699">Spent today</td>
        <td style="border: 1px solid #336699">Calls today</td>
        <td style="border: 1px solid #336699">Time Today</td>
      </tr>
 
<tr style="background-color: #E0E8E6 ">
<td align="center"> <b>$<? echo number_format($response1[balance][current_balance],2);   ?> </td>
<td align="center"> $<? echo number_format($response1[balance][spent_total],3);   ?> </td>
<td align="center"> <? echo $response1[balance][calls_total]   ?> </td>
<td align="center"> <? echo $response1[balance][time_total]   ?> </td>
<td align="center"> $<? echo number_format($response1[balance][spent_today],3);   ?> </td>
<td align="center"> <? echo $response1[balance][calls_today]   ?> </td>
<td align="center"> <? echo $response1[balance][time_today]   ?> </td>
 
</table>
 
<table cellspacing="2" cellpadding="6" style="border: 1px solid #336699;font-size: 12px;font-family:Arial">
    <tr style="background-color: #D7F3EC">
        <td style="border: 1px solid #336699"> Date</td>
        <td style="border: 1px solid #336699">CallerID</td>
        <td style="border: 1px solid #336699"> Destination</td>
        <td style="border: 1px solid #336699">Description</td>
        <td style="border: 1px solid #336699">Disposition</td>
        <td style="border: 1px solid #336699">Duration</td>
        <td style="border: 1px solid #336699">Rate</td>
        <td style="border: 1px solid #336699">Total</td>
    
    </tr>
    <?
    foreach($cdr as $call){
        ?>
        <tr style="background-color: #E0E8E6 ">
            <td align="right">
                <? 
if (substr($call[date],0,10) == substr($prev_call,0,10)) 
echo substr( $call[date],11);               
else
                  echo $call[date]; ?>
            </td>
            <td><? echo $call[callerid]; ?></td>
            <td align="right"> <? echo formatPhone($call[destination]); ?> </td>
            <td> <? echo $call[description]; ?> </td>
            <td>
                <? 
if ( $call[disposition]=='FAILED') echo "<font color='#FF0000'>";    
else if  ( $call[disposition]=='NO ANSWER') echo "<font color='#777777'>";            
                echo $call[disposition]; ?>
            </td>
            <td align="right"> <? echo $call[duration]; ?> </td>
            <td align="right">
                <?
                if ($call[rate] > 0) echo "<b>"; 
                echo number_format($call[rate],4); ?>
            </td>
            <td align="right">
                <? 
                if ($call[total] > 0) echo "<b>"; 
                echo number_format($call[total],3); ?>
            </td>
        </tr>
        <?
$prev_call = $call[date];
    }
    ?>
</table>
 
<?
 
// totals of the listed calls
//echo "Calls: <b>{$calls}</b> - Duration: <b>{$duration}</b> - Total: <b>\$".number_format($total,3)."</b><br>";
 
/* Function to Calculate Time */
function secToTime($secs){
    if(!$secs){return 0;}
    
    $vals = array('h' => floor($secs / 3600), 
                  'm' => floor($secs % 3600 / 60), 
                  's' => $secs % 60); 
 
    $ret = array(); 
 
    $added = false; 
    foreach ($vals as $k => $v) { 
        if ($v > 0 || $added) { 
            $ret[] = ((strlen($v) == 1) && $added) ? "0$v" : $v; 
            $added = true;
        } 
    } 
 
    return join(':', $ret); 
}
 
    function formatPhone($phone = '')
    {
    // If we have not entered a phone number just return empty
    if (empty($phone)) {
    return '';
    }
    
    // Strip out any extra characters that we do not need only keep letters and numbers
    $phone = preg_replace("/[^0-9A-Za-z]/", "", $phone);
               
    // Perform phone number formatting here
    if (strlen($phone) == 7) {
    return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$1-$2", $phone);
    } elseif (strlen($phone) == 10) {
    return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$1-$2-$3", $phone);
    } elseif (strlen($phone) == 11) {
    return preg_replace("/([0-9a-zA-Z]{1})([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$1-$2-$3-$4", $phone);
    }    elseif (strlen($phone) == 15) {
    return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})([0-9a-zA-Z]{4})([0-9a-zA-Z]{4})/", "$1-$2-$3-$4", $phone);
    }  
    // Return original phone if not 7, 10, 11 or 15 digits long
    return $phone;
    }
    
?>