freeswitch php,PHP Event Socket

About

The code below was last edited in 2008, but is still working in 2013 on php v5.3

Other implementation in PHP5:

Click here to expand Table of Contents

PHP client examle

Website based PHP event socket example. Make sure to change $host to the ip address of your FreeSWITCH server.

$password = "ClueCon";

$port = "8021";

$host = "127.0.0.1";

function event_socket_create($host, $port, $password) {

$fp = fsockopen($host, $port, $errno, $errdesc)

or die("Connection to $host failed");

socket_set_blocking($fp,false);

if ($fp) {

while (!feof($fp)) {

$buffer = fgets($fp, 1024);

usleep(100); //allow time for reponse

if (trim($buffer) == "Content-Type: auth/request") {

fputs($fp, "auth $password\n\n");

break;

}

}

return $fp;

}

else {

return false;

}

}

function event_socket_request($fp, $cmd) {

if ($fp) {

fputs($fp, $cmd."\n\n");

usleep(100); //allow time for reponse

$response = "";

$i = 0;

$contentlength = 0;

while (!feof($fp)) {

$buffer = fgets($fp, 4096);

if ($contentlength > 0) {

$response .= $buffer;

}

if ($contentlength == 0) { //if contentlenght is already don't process again

if (strlen(trim($buffer)) > 0) { //run only if buffer has content

$temparray = split(":", trim($buffer));

if ($temparray[0] == "Content-Length") {

$contentlength = trim($temparray[1]);

}

}

}

usleep(100); //allow time for reponse

//optional because of script timeout //don't let while loop become endless

if ($i > 10000) { break; }

if ($contentlength > 0) { //is contentlength set

//stop reading if all content has been read.

if (strlen($response) >= $contentlength) {

break;

}

}

$i++;

}

return $response;

}

else {

echo "no handle";

}

}

$fp = event_socket_create($host, $port, $password);

$cmd = "api help";

$response = event_socket_request($fp, $cmd);

echo $response;

fclose($fp);

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值