The intent of this PHP is to loop through an array returned by an API call and insert the resulting rows into a mySQL table. The array is declared as $data.
The example array provided has two call records [0] and [1] however it can return no call records and many call records depending on caller volume:
Array (
[call] => Array (
[0] => Array (
[hostname] => host.domain.net
[session_id] => 20221001155343061364
[orig_callid] => 0gip97admc1f6s4vbaok
[orig_match] => sip:user@domain.com
[orig_user] => 2001
[orig_domain] => domain.com
[orig_uri] => sip:tel@domain.com
[orig_name] => Peake
[ani] => tel
[dnis] => tel
[by_action] => Array ( )
[by_user] => Array ( )
[by_domain] => Array ( )
[by_callid] => Array ( )
[term_callid] => 20221001155343061365-f1ce8fd3cd1efdb3750535b87e7b7b79
[term_user] => Array ( )
[term_domain] => *
[term_uri] => sip:12028410090@host
[time_begin] => 2022-10-01 15:53:43
[time_answer] => 0000-00-00 00:00:00
[orig_call_info] => progressing
[term_call_info] => alerting
[media] => G.711 u-law
)
[1] => Array (
[hostname] => host.domain.net
[session_id] => 20221001155348027249
[orig_callid] => 2311si08u8u8m5udenak
[orig_match] => sip:user@domain.com
[orig_user] => 2001
[orig_domain] => domain.com
[orig_uri] => sip:tel@domain.com
[orig_name] => Peake
[ani] => tel
[dnis] => tel
[by_action] => Array ( )
[by_user] => Array ( )
[by_domain] => Array ( )
[by_callid] => Array ( )
[term_callid] => 20221001155348027250-f1ce8fd3cd1efdb3750535b87e7b7b79
[term_user] => Array ( )
[term_domain] => *
[term_uri] => sip:tel@host
[time_begin] => 2022-10-01 15:53:48
[time_answer] => 2022-10-01 15:53:50
[orig_call_info] => active
[term_call_info] => active
[media] => PCMU
)
)
)
Here is my PHP:
<?php
foreach($data as $i => $item) {
$link = mysqli_connect("127.0.0.1", "db", "password", "table");
$sql = "INSERT INTO ns_cdr (session_id,orig_user, dnis, ani, time_begin)
VALUES ('".$data[$i]['session_id']."',
'".$data[$i]['orig_match']."',
'".$data[$i]['dnis']."',
'".$data[$i]['ani']."',
'".$data[$i]['time_begin']."')
ON DUPLICATE KEY UPDATE
orig_user = '".$data[$i]['orig_match']."',
dnis = '".$data[$i]['dnis']."',
ani = '".$data[$i]['ani']."',
time_begin='".$data[$i]['time_begin']."'
";
if(mysqli_query($link, $sql)){
echo "";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
//}
}
?>
This is the result:
Notice: Undefined index: session_id in /var/www/html/tool_getcalls.php on line 50
Notice: Undefined index: orig_match in /var/www/html/tool_getcalls.php on line 51
Notice: Undefined index: dnis in /var/www/html/tool_getcalls.php on line 52
Notice: Undefined index: ani in /var/www/html/tool_getcalls.php on line 53
Notice: Undefined index: time_begin in /var/www/html/tool_getcalls.php on line 54
Notice: Undefined index: orig_match in /var/www/html/tool_getcalls.php on line 56
Notice: Undefined index: dnis in /var/www/html/tool_getcalls.php on line 57
Notice: Undefined index: ani in /var/www/html/tool_getcalls.php on line 58
Notice: Undefined index: time_begin in /var/www/html/tool_getcalls.php on line 59
Just not sure how to handle the loop correctly to parse the array contents. Any help is appreciated!