asterisk-queue-manager/php/ami_get.php

79 lines
1.6 KiB
PHP
Raw Normal View History

2023-07-31 14:50:26 +02:00
<?php
include('../config.php');
header('Content-Type: application/json');
// Open socket to ami server
$socket = fsockopen($ami_host, $ami_port);
// Login
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: " . $ami_user . "\r\n");
fputs($socket, "Secret: " . $ami_pass . "\r\n\r\n");
//
// Action: SIPpeers -> $sippeers
//
$sippeers = [];
$extension = '';
$extension_status = 0;
fputs($socket, "Action: SIPpeers\r\n\r\n");
while(($data = fgets($socket, 8096)) != "Event: PeerlistComplete\r\n") {
$pattern_extension = '/^ObjectName:\s(\d\d\d).*$/';
if(preg_match_all($pattern_extension, $data, $match)) {
$extension = $match[1][0];
}
$pattern_ipaddress = '/^IPaddress.*$/';
if(preg_match($pattern_ipaddress, $data)) {
$pattern_onlinestatus = '/^IPaddress:\s(-none-).*$/';
if(preg_match_all($pattern_onlinestatus, $data, $match)) {
$extension_status = '0';
} else {
$extension_status = '1';
}
$sippeers[$extension] = $extension_status;
}
}
//
// Action: QueueStatus -> $queuemembers
//
// TODO: Status: x
// 2 In Use
// 6 Ringing
$queuemembers = [];
fputs($socket, "Action: QueueStatus\r\n");
fputs($socket, "Queue: warteschlange\r\n\r\n");
while(($data = fgets($socket, 8096)) != "Event: QueueStatusComplete\r\n") {
$pattern_name = '/^Name.*$/';
if(preg_match($pattern_name, $data)) {
$pattern_extension = '/^Name:\sSIP\/(\d\d\d).*$/';
if(preg_match_all($pattern_extension, $data, $match)) {
array_push($queuemembers, $match[1][0]);
}
}
}
// Close socket
fclose($socket);
//
// Create JSON data package
//
echo json_encode(
array(
'sippeers' => $sippeers,
'queuemembers' => $queuemembers,
),
);
?>