-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamiXml2Array.php
More file actions
211 lines (177 loc) · 6.14 KB
/
amiXml2Array.php
File metadata and controls
211 lines (177 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Simple classe de conexao e comandos(actions) ami utilizando php
* Para habilitar , em manager.conf
* enabled = yes
* webenabled = yes
* Observar a config permit
* Ex liberar acesso para o ip 192.168.1.10
* permit = 192.168.1.10/255.255.255.0
* usuario de exemplo em manager.conf
* [teste_asterisk]
* secret = asterisk
* read = call,dialplan
* write = system,all
*
* https://wiki.asterisk.org/wiki/display/AST/Allow+Manager+Access+via+HTTP
*/
class AmiXml2Array
{
public $curl,$pathTmpFile,$loginUrl,$globalUrl,$options;
/**
* @access protected
* Configuracoes das opcoes do curl
*/
public function __construct()
{
$this->curl = $this->curlStart();
$tempFile = tmpfile();
$this->pathTmpFile = stream_get_meta_data($tempFile)['uri'];
$this->loginUrl = '192.168.1.10:8088/mxml?action=login&username=teste_asterisk&secret=asterisk';
$this->globalUrl = '192.168.1.10:8088/mxml?';
$this->options = [
CURLOPT_COOKIESESSION => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_COOKIEFILE => $this->pathTmpFile
];
}
public function curlStart(){
return curl_init();
}
/**
* Faz login no manager utilizando curl
*/
public function loginAmiHttp()
{
$this->options[CURLOPT_URL] = $this->loginUrl;
$curl = $this->curl;
curl_setopt_array($curl, $this->options);
$pathTmpFile = $this->pathTmpFile;
$data = curl_exec($curl);
$xml = simplexml_load_string($data);
$nodes = [];
foreach ($xml as $node) {
$nodes[] = current($node->generic->attributes());
}
return compact('curl','pathTmpFile');
}
/**
* Faz logoff do manager
*/
public function logoffAmiHttp()
{
$curl = $this->curl;
$this->options[CURLOPT_URL] = $this->globalUrl . 'action=logoff';
$this->options[CURLOPT_COOKIEFILE] = '';
curl_setopt_array($this->curl, $this->options);
$data = curl_exec($this->curl);
return $data;
}
/**
* Retorna um array com informacoes referentes a action consultada
* @param string $action - Ver em https://wiki.asterisk.org/wiki/display/AST/Asterisk+14+AMI+Actions
*/
public function getSimpleAction($action,$object = false){
$curlData = $this->loginAmiHttp();
if(!$curlData){
return false;
}
$curl = $curlData['curl'];
$tmpFile = $curlData['pathTmpFile'];
$url = $this->globalUrl . "action={$action}";
$this->options[CURLOPT_URL] = $url;
curl_setopt_array($curl, $this->options);
$data = curl_exec($curl);
$this->logoffAmiHttp();
$xml = simplexml_load_string($data);
$nodes = [];
foreach ($xml as $node) {
$nodes[] = current($node->generic->attributes());
}
if($object){
return json_encode($nodes);
}
return $nodes;
}
/**
* Retorna um array com informacoes sobre a fila
* @param string $queuName - O nome da fila
*/
public function getQueueStatusByName($queueName,$object = false){
$curlData = $this->loginAmiHttp();
if(!$curlData){
return false;
}
$curl = $curlData['curl'];
$tmpFile = $curlData['pathTmpFile'];
$url = $this->globalUrl . "action=QueueStatus&queue={$queueName}";
$this->options[CURLOPT_URL] = $url;
curl_setopt_array($curl, $this->options);
$data = curl_exec($curl);
$this->logoffAmiHttp();
$xml = simplexml_load_string($data);
$nodes = [];
foreach ($xml as $node) {
$nodes[] = current($node->generic->attributes());
}
if($object){
return json_encode($nodes);
}
return $nodes;
}
/**
* Retorna um array com informacoes sobre a fila
* @param string $sipPeeer - O peer a ser consultado(ex:1004)
*/
public function getSipStatusBySipPeer($sipPeeer,$object = false){
$curlData = $this->loginAmiHttp();
if(!$curlData){
return false;
}
$curl = $curlData['curl'];
$tmpFile = $curlData['pathTmpFile'];
$url = $this->globalUrl . "action=SIPshowpeer&peer={$sipPeeer}";
$this->options[CURLOPT_URL] = $url;
curl_setopt_array($curl, $this->options);
$data = curl_exec($curl);
$this->logoffAmiHttp();
$xml = simplexml_load_string($data);
$nodes = [];
foreach ($xml as $node) {
$nodes[] = current($node->generic->attributes());
}
if($object){
return json_encode($nodes);
}
return $nodes;
}
}
/**
* More actions
* https://wiki.asterisk.org/wiki/display/AST/Asterisk+14+AMI+Actions
* Examples
*/
$ami = new AmiXml2Array();
//get status from all sip peers
//json
print_r($ami->getSimpleAction('SIPpeers',true));
//array
print_r($ami->getSimpleAction('SIPpeers'));
//queue status from all queues
//json
print_r($ami->getSimpleAction('QueueStatus',true));
//array
print_r($ami->getSimpleAction('QueueStatus'));
// // //queue status from callcenter_1_fila_asdfasd_10
//json
print_r($ami->getQueueStatusByName('callcenter_1_fila_asdfasd_10',true));
//array
print_r($ami->getQueueStatusByName('callcenter_1_fila_asdfasd_10'));
//get status from peer 11004
//json
print_r($ami->getSipStatusBySipPeer('11004',true));
//array
print_r($ami->getSipStatusBySipPeer('11004'));