PHP注意:未定义索引:在第72行调用/var/www/swvx/timedcdr2.php

时间:2011-07-22 03:35:59

标签: php

我在以下代码中收到此错误,并且无法弄清楚原因。我认为这是两次$ call循环,但我不知道为什么。有人想给我一个手部校对吗?

PHP注意:未定义索引:在第72行调用/var/www/swvx/timedcdr2.php

谢谢!

<?
//to be polled every 30 seconds.  should first get a list of accoutn IDs, then use them to get the CDR for the last day.
//include switchvox libraries
require_once("SwitchvoxRequest.php");

//define sql connection stuff
$db_host = "host";
$db_user = "user";
$db_pass = "secret";
$db = "db";
$table_sr = "tblSalesReps";
$table_cd = "tblCallsMadeReceived_raw";
$link = mssql_connect($db_host, $db_user, $db_pass);

//make sure we can connect
if (!$link || !mssql_select_db($db, $link)) {
        die('Unable to connect or select database!');
        }

//define pbx connection stuff
$sv_host = "url";
$sv_user = "user";
$sv_pass = "secret";

//query the salesrep table to find the account IDs available
$acid_sql = "SELECT * FROM $table_sr WHERE [pbx_accountid] > 0";
$acid_res = mssql_query($acid_sql);

//get and format the time and date as YYYY-MM-DD, format the time as HH:MM:SS
$date = date('Y') . "-" . date('m') . "-" . date('d');
$time = date('H') . ":" . date('i') . ":" . date('s');

//take only the last hour of results, rather than an entire day
$st_time = date('H')-1 . ":" . date('i') . ":" . date('s');

echo "<pre>";

while ($row = mssql_fetch_array($acid_res, MSSQL_ASSOC)) {
        $req = new SwitchvoxRequest($sv_host, $sv_user, $sv_pass);
        $reqpar = array
                (
                'account_ids' => array
                        (
                        'account_id' => array
                                (
                                $row['pbx_accountid']
                                )
                        ),
                'start_date' => array
                        (
                        $date . " " . $st_time
                        ),
                'end_date' =>  array
                        (
                        $date . " " . $time
                        ),
                'sort_field' => array
                        (
                        ),
                'sort_order' => array
                        (
                        'DESC'
                        )
                );

        $res = $req -> send("switchvox.callLogs.search", $reqpar);
        $result = $res->getResult();
        $calls = $result['calls']['total_items'];
        print_r($calls);
        print_r($row['pbx_accountid']);
        echo "<br><br>";
        if($result['calls']['call']) {   //<====LINE 72 ################################
                foreach($result['calls']['call'] as $call) {
                        $id = $call['id'];
                        //check to see if the call has already been logged
                        $id_sql = "SELECT * FROM $table_cd WHERE callID='$id'";
                        $id_res = mssql_query($id_sql);
                        $exid = mssql_fetch_array($id_res, MSSQL_ASSOC);

                        if($exid['callID']) {
                                //print_r($exid);  //uncomment to show duplicate results
                                } elseif (!$exid['callID']) {
                                        //print_r($call);  //uncomment to show new results
                                        //varialbes to insert
                                        $from = $call['from_number'];
                                        $to = $call['to_number'];
                                        $durat = $call['talk_duration'];
                                        $start = $call['start_time'];
                                        $callid = $call['id'];
                                        $calltype = $call['origination'];
                                        //set the proper values into extension/phonenumber
                                        if($calltype == "outgoing") {
                                                $extension = $from;
                                                $phonenumber = $to;
                                                } else {
                                                        $extension = $to;
                                                        $phonenumber = $from;
                                                }
                                        //insert the data into the table
                                        $fi_sql = "INSERT INTO $table_cd (extension, phonenumber, calldatetime, duration, callID, calltype) VALUES ($extension, $phonenumber, '$start', '$durat', '$callid', '$calltype')";
                                        $fi_res = mssql_query($fi_sql);
                                                }
                                }
                }
}
?>

编辑 - 它确实有效;它偶尔会抛出那个错误。至少我觉得它有效。我假设它可能会到达pbx_accountid数组的最后结果而不再有?我将计算它的次数,看它是否对应。

编辑 - 是的,每次运行时都会出现20次错误,并且有20个帐户ID。如果没有更多的呼叫索引,我如何强制它停止?

1 个答案:

答案 0 :(得分:3)

更改此

if($result['calls']['call']) {   //<====LINE 72 ################################

到此

if(isset($result['calls']['call'])) {   //<====LINE 72 ################################

该通知表示您正在读取不存在的变量。您这样做是为了检查变量是否存在。有一个函数可以检查变量是否存在而不尝试读取它isset