我使用自定义类通过php连接到星号服务器。下面是代码:
class Asterisk_ami
{
public $ini = array();
function __construct ()
{
$this->ini["con"] = false;
$this->ini["host"] = "127.0.0.1";
$this->ini["port"] = "****";
$this->ini["lastActionID"] = 0;
$this->ini["lastRead"] = array();
$this->ini["sleep_time"]=1.5;
$this->ini["login"] = "****";
$this->ini["password"] = "****";
}
function __destruct()
{
unset ($this->ini);
}
public function connect()
{
$this->ini["con"] = fsockopen($this->ini["host"], $this->ini["port"],$a,$b,10);
if ($this->ini["con"])
{
stream_set_timeout($this->ini["con"], 0, 400000);
}
}
public function disconnect()
{
if ($this->ini["con"])
{
fclose($this->ini["con"]);
}
}
public function write($a)
{
$this->ini["lastActionID"] = rand (10000000000000000,99999999900000000);
fwrite($this->ini["con"], "ActionID: ".$this->ini["lastActionID"]."rn$arnrn");
$this->sleepi();
return $this->ini["lastActionID"];
}
public function sleepi ()
{
sleep($this->ini["sleep_time"]);
}
public function read()
{
$mm = array();
$b = array();
$mmmArray=array();
$k = 0;
$s = "";
$this->sleepi();
do
{
$s.= fread($this->ini["con"],1024);
sleep(0.005);
$mmm=socket_get_status($this->ini["con"]);
array_push($mmmArray, $mmm);
} while ($mmm['unread_bytes']);
$mm = explode ("rn",$s);
$this->ini["lastRead"] = array();
for ($i=0;$i<count($mm);$i++)
{
if ($mm[$i]=="")
{
$k++;
}
$m = explode(":",$mm[$i]);
if (isset($m[1]))
{
$this->ini["lastRead"][$k][trim($m[0])] = trim($m[1]);
}
}
unset ($b);
unset ($k);
unset ($mm);
unset ($mm);
unset ($mmm);
unset ($i);
unset ($s);
var_dump($mmmArray);
return $this->ini["lastRead"];
//return $s;
}
public function init()
{
return $this->write("Action: LoginrnUsername: ".$this->ini["login"]."rnSecret: ".$this->ini["password"]."rnrn");
}
}
这里是testAsterisk.php,我在这里尝试。
include("./lib/asteriskAmi.php");
$a = new Asterisk_ami();
$a->connect();
if ($a->ini["con"])
{
$a->init();
$a->write("Action: GetConfigrnFilename: extensions.confrn");
print_r($a->read());
$a->disconnect();
}
我想通过ami获得extension.conf配置。问题是我没有得到完整的配置。16 .最后一串总是不见了。然而,当我通过星号控制台检查GetConfig时,它返回完整的配置。正如您所看到的,当socket_get_status
的未读字节为0时,循环被中断,我检查了它们在数组中推入并转储它,并且我可以看到unread_bytes实际上是0。我尝试改变sleep_time
和不同的超时参数,结果是一样的。我还能查些什么?是什么导致了这个错误?也许我可以使用其他功能?
实际上,我找到的唯一合适的解决方案是使用PAMI,而不是自定义类。它使用起来更舒服,而且它给了我extensions.conf的完整内容。