-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcall.php
83 lines (79 loc) · 3.35 KB
/
call.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
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
<?php
## AUTHOR: Alisson Pelizaro ([email protected])
// Replace with your server settings if not using the default.
// If unsure check /etc/asterisk/manager.conf under [general];
$ipServer = "172.0.0.1";
$port = "5038";
$protocolServer = "tcp";
// Replace with your username. You can find it in /etc/asterisk/manager.conf.
// If unsure look for a user with "originate" permissions, or create one as
$username = "user";
// Replace with your password (refered to as "secret" in /etc/asterisk/manager.conf)
$password = "secret";
// Internal phone line to call from
$internalPhoneline = "1003";
$ws = false; //Set 'true' if you use WebSocket protocol transport in your Asterisk Server
// Context for outbound calls. See /etc/asterisk/extensions.conf if unsure.
$context = '1-GRam-Grupo_1';
//Starting script
if($ws){
$port .= "/ws";
}
$socket = stream_socket_client("$protocolServer://$ipServer:$port");
$target = "1006";
if($socket)
{
echo "Connected to socket, sending authentication request.\n";
// Prepare authentication request
$authenticationRequest = "Action: Login\r\n";
$authenticationRequest .= "Username: $username\r\n";
$authenticationRequest .= "Secret: $password\r\n";
$authenticationRequest .= "Events: off\r\n\r\n";
// Send authentication request
$authenticate = stream_socket_sendto($socket, $authenticationRequest);
if($authenticate > 0)
{
// Wait for server response
usleep(200000);
// Read server response
$authenticateResponse = fread($socket, 4096);
// Check if authentication was successful
if(strpos($authenticateResponse, 'Success') !== false)
{
echo "Authenticated to Asterisk Manager Inteface. Initiating call.\n";
// Prepare originate request
$originateRequest = "Action: Originate\r\n";
$originateRequest .= "Channel: SIP/$internalPhoneline\r\n";
$originateRequest .= "Callerid: Alisson\r\n";
$originateRequest .= "Exten: $target\r\n";
$originateRequest .= "Context: $context\r\n";
$originateRequest .= "Priority: 0\r\n";
$originateRequest .= "Async: yes\r\n\r\n";
// Send originate request
$originate = stream_socket_sendto($socket, $originateRequest);
if($originate > 0)
{
// Wait for server response
usleep(200000);
// Read server response
$originateResponse = fread($socket, 4096);
// Check if originate was successful
if(strpos($originateResponse, 'Success') !== false)
{
echo "Call initiated, dialing.";
} else {
echo "Could not initiate call.\n";
}
} else {
echo "Could not write call initiation request to socket.\n";
}
} else {
echo "Could not authenticate to Asterisk Manager Interface.\n";
}
} else {
echo "Could not write authentication request to socket.\n";
}
} else {
echo "Unable to connect to socket.";
}
?>