【发布时间】:2017-11-04 07:31:46
【问题描述】:
我正在使用 Codeigniter 框架 (php-jquery) 开发一个联系人管理系统,并且我正在使用 Asterisk(11 或更高版本)来管理呼叫。
在这个系统中,有来电和去电。在拨出电话的情况下,我想通过单击一个按钮给某人打电话,在来电的情况下,我想在发生这种情况时将它们显示为通知弹出窗口。为此,我正在编写一个库类,如下所示,它只管理传出呼叫。
class Asterisk {
public $server;
public $port;
public $socket;
public $error;
const NOSOCKET = 'No Socket Defined';
const CONNECTFAILED = 'Connection Failed';
const AUTHFAIL = 'Authentication Failed';
const NORESP = 'Server Didn\'t Respond';
public function __construct($params = array()) {
$this->server = $_SERVER['HTTP_HOST'];
$this->port = 5038;
if (isset($params['server']))
$this->server = $params['server'];
if (isset($params['port'])) {
$this->port = $params['port'];
}
}
private function _check() {
if ($this->socket)
return true;
$this->error = Asterisk::NOSOCKET;
redirect('');
}
private function _command($query, $expect = null, $error = null) {
$this->_check();
fputs($this->socket, $query."\r\n");
$response = fgets($this->socket);
if (!$response) {
$this->error = Asterisk::NORESP;
return false;
}
if ($expect == null)
return true;
if (strpos($response, $expect) != false)
return true;
$this->error = $error;
return false;
}
/* ************************************************************************************************************** */
public function connect($server = null, $port = null) {
if ($this->socket)
$this->close();
if ($server != null && $port != null) {
$this->server = $server;
$this->port = $port;
}
$this->socket = fsockopen($this->server, $this->port, $errno, $errstr, 1);
if (!$this->socket) {
$this->error = Asterisk::CONNECTFAILED . " - $errstr ($errno)";
return false;
}
stream_set_timeout($this->socket, 3);
return true;
}
public function close() {
$this->_check();
fclose($this->socket);
return true;
}
public function login($username, $password) {
return $this->_command(
"Action: Login\r\n".
"UserName: $username\r\n".
"Secret: $password\r\n".
"Events: off\r\n",
"Message: Authentication accepted",
Asterisk::AUTHFAIL
);
}
public function logout() {
return $this->_command(
"Action: Logoff\r\n"
);
}
/* ************************************************************************************************************** */
public function call($channel, $context, $extension, $callerId, $priority = 1, $async = true, $timeout = 30000) {
return $this->_command(
"Action: Originate\r\n".
"Channel: $channel\r\n".
"Context: $context\r\n".
"Exten: $extension\r\n".
"Priority: $priority\r\n".
"Async: $async\r\n".
"CallerId: $callerId\r\n".
"TimeOut: $timeout\r\n"
);
}
}
如何更新此类以处理来电及其事件?
我知道有一些现有的库,例如 PAMI,但它不能与 codeigniter 和 mvc 模型一起正常工作。
如何管理这些来电?有人可以发布一些示例代码吗?
谢谢。
【问题讨论】:
标签: php codeigniter event-handling asterisk