How to establish a persistent connection to the Asterisk manager with PHP?
I am trying to connect to the Asterisk manager interface and I have an issue with blocking code as well as connection persistence. Below is what I have, followed by a description of what is going wrong:
/**
* The parameters for connecting to the server
*/
$params = array('server' => '192.168.1.100', 'port' => '5038');
/**
* Instantiate Asterisk object and connect to server
*/
$ast = new Net_AsteriskManager($params);
/**
* Connect to server
*/
try {
$ast->connect();
} catch (PEAR_Exception $e) {
echo $e;
}
/**
* Login to manager API
*/
try {
$ast->login('admin', 'abcdefghi');
} catch(PEAR_Exception $e) {
echo $e;
}
The above code works as much as possible. I can get data through it.
The problem in sending the request is taking quite a long time and when I watch the server in real time (console) I see that the user admin is logging out of the server after sending the output.
In other words, "admin" is logged out, although I was not explicitly logged out in the code. How can I make this connection persistent?
The Asterisk AMI does not close the connection automatically, however it is the network layer that it does, when it detects no activity for a long time (= timeout) it drops the connection. To make the connection persistent, it needs to be busy (= keep alive), whenever the connection is idle, your application should send the saved packets to the target server at the specified interval (= TTL). We can use any type of command as a saved packet, for example in an asterisk that you can use "Ping".
However, if you are looking for some existing ready-to-use solution, you can use AMI Proxy for that. here are some famous AMI proxies
I think you are just using the php-agi.php class. it already has everything you need. There is no need to write again.
php-agi.php is distributed with any asterisk and can be found in / var / lib / asterisk / agi-bin /