-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathami.php
55 lines (44 loc) · 1.36 KB
/
ami.php
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
<?php
class AMI
{
private $socket = 0;
private $p_Server, $p_Port, $p_Username, $p_Password;
public function __construct($p_Server, $p_Port, $p_Username, $p_Password) {
$this->p_Server = $p_Server;
$this->p_Port = $p_Port;
$this->p_Username = $p_Username;
$this->p_Password = $p_Password;
}
public function sendCommand($cmd)
{
if ( !fwrite($this->socket, $cmd) )
return "Error writing to socket\n";
$response = stream_get_contents($this->socket);
//print $response;
return $response;
}
public function login()
{
// Create network socket
$this->socket = fsockopen($this->p_Server, $this->p_Port);
stream_set_timeout($this->socket, 0, 100000); // 0.1 sec
// Login via Asterisk Manager API
$cmd = "Action: login\r\n"
."Username: {$this->p_Username}\r\n"
."Secret: {$this->p_Password}\r\n\r\n";
$response = $this->sendCommand($cmd);
//print $response;
if ( strpos($response, "Message: Authentication accepted") == false )
{
echo "Error authenticating server: $response";
exit;
}
}
public function logoff()
{
$cmd = "Action: Logoff\r\n\r\n";
$response = $this->sendCommand($cmd);
fclose($this->socket);
}
}
?>