Ending a call using a php script

I’ve been testing alternatives to do this.
I make my calls using .call files, and now I wish that once the call is in progress, can be hung using a button on a web page.

Is this possible?
I use AGI or AMI?

Please help I am a bit lost!

Thanks!

Hi

As you initiate the calls with a call file its a bit more complicated, couldn’t you migrate to using the AMI to start the calls, Then as we do you store the channel in a DB and recall it to hang it up. all done in PHP

Ian

Thanks Ian!

I’ll try that and then tell you about my experience.

Hi!

After a little research on AMI and its operation. I implemented a script to make a call to the PSTN.
I tried also to create a script to hang once the call is in progress but I have not been successful.

This is the code to make the call:

[code]<?php

$strHost = “192.168.1.x”;
$strUser = “user”;
$strSecret = “secret”;
$strContext = “context”;
$strWaitTime = 45;
$strPriority = 1;

$strMaxRetry = “2”;

$number = “103”;

$strChannel = “DAHDI/g0/########”; //This is the number to which I call

if ($strChannel != “”) :
$errno = 0;
$errstr = 0;
$strCallerId = " WebCall<$number>";
$oSocket = fsockopen($strHost, 5038, $errno, $errstr, 20);
if (!$oSocket) {
echo “$errstr ($errno)\n”;
} else {

    fputs($oSocket, "Action: login\r\n");
    fputs($oSocket, "Events: off\r\n");
    fputs($oSocket, "Username: $strUser\r\n");
    fputs($oSocket, "Secret: $strSecret\r\n\r\n");
    fputs($oSocket, "Action: originate\r\n");
    fputs($oSocket, "Channel: $strChannel\r\n");
    fputs($oSocket, "WaitTime: $strWaitTime\r\n");
    fputs($oSocket, "CallerId: $strCallerId\r\n");
    fputs($oSocket, "Exten: $number\r\n");
    fputs($oSocket, "Context: $strContext\r\n");
    fputs($oSocket, "Priority: $strPriority\r\n\r\n");
    fputs($oSocket, "Action: Logoff\r\n\r\n");
    sleep(2);
    fclose($oSocket);
}
if (!isset($_REQUEST['Exten'])) {
    echo "";
} else {
    echo "$strChannel enlazando a $number.";
}

else :
exit();
endif;
?>[/code]

And this implementation was the one who tried to use to hang up (without success):

http://phylevn.mexrom.net/index.php/blog/show/226.html

Does anyone know of any way to do this, or any references?

Hi after establishing teh call with AMI, we store the result as an array, and pull it apart

Then storing the relevent sections in teh DB

echo “CHannel is “;
echo $channel;
//check for entry in chanmap
$query = “select * from chanmap where agent = '”.$agent.”’”;
$res = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($res) > 0){
$nquery = “update chanmap set chan = '”.$channel."’ where agent = ‘".$agent."’";
}else{
$nquery = “insert into chanmap (chan,agent)values(’”.$channel."’,’".$agent."’)";
}
mysql_query($nquery) or die(mysql_error());
$cdrquery = “insert into cdr (destnum,destnam,agent,sipchan)values(’”.$destnum."’,’".$destname."’,’".$agent."’,’".$channel."’)";
mysql_query($cdrquery) or die(mysql_error());
}elseif($action == “hangup” && isset($agent)){
echo $agent;
hang_up_agent($sock,$agent);
}

}

function hang_up_agent(&$sock,$agent){
$query = “select chan from chanmap where agent = '”.$agent."’";
$res = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($res) > 0){
$r = mysql_fetch_row($res);

            $chan = $r[0];
            $clearMessage = "Action: Hangup\r\n";
            $clearMessage .= "Channel: $chan\r\n\r\n";

echo $clearMessage;
socket_write($sock,$clearMessage);
}
}

This showuld give you something to go on. also look at teh code fro teh ARI voicemail interface as this does hangup as well.

Thanks Ian!

I just dont understand how to recover the channel after making the call.
Do you use fread when you make the call?

With this code I can hang up the call but still do not know how to know the channel where the call originated!

[code]$amiHost = “192.168.1.x”;
$amiUser = “user”;
$amiSecret = “secret”;
$amiChannel = “HOW I CAN KNOW?”;

if ($amiChannel == null) :
exit();
endif;

$errno = 0;
$errstr = 0;
$oSocket = fsockopen($amiHost, 5038, $errno, $errstr, 20);
if (!$oSocket) {
echo “$errstr ($errno)
\n”;
} else {

fputs($oSocket, "Action: login\r\n");
fputs($oSocket, "Events: off\r\n");
fputs($oSocket, "Username: $amiUser\r\n");
fputs($oSocket, "Secret: $amiSecret\r\n\r\n");

fputs($oSocket, "Action: Hangup\r\n");
fputs($oSocket, "channel: $amiChannel\r\n\r\n");
echo "hanging channel: $amiChannel \r\n";

fputs($oSocket, "Action: Logoff\r\n\r\n");
sleep(2);

fclose($oSocket);

}[/code]

Well, I did what I need.
I can make a call using PHP scripts (AMI), and I can also finalized the same way.
Maybe not the most elegant way to do it, but that’s how I did it to hang the call:

Script to make the call:

[code]function callNow_AMI($phone_number) {

    $strHost = "192.168.1.x";
    $strUser = "user";
    $strSecret = "secret";
    $strContext = "context";
    $strWaitTime = 45;
    $strPriority = 1;
    $strMaxRetry = "2";

    $number = "s";
    $strChannel = "DAHDI/g0/" . $phone_number;

    $channel = "";

    $errno = 0;
    $errstr = 0;
    $strCallerId = " WebCall<$number>";
    $oSocket = fsockopen($strHost, 5038, $errno, $errstr, 20);
    if (!$oSocket) {
        echo "$errstr ($errno)\n";
    } else {

        fputs($oSocket, "Action: login\r\n");
        fputs($oSocket, "Events: on\r\n");
        fputs($oSocket, "Username: $strUser\r\n");
        fputs($oSocket, "Secret: $strSecret\r\n\r\n");
        fputs($oSocket, "Action: originate\r\n");
        fputs($oSocket, "Channel: $strChannel\r\n");
        fputs($oSocket, "WaitTime: $strWaitTime\r\n");
        fputs($oSocket, "CallerId: $strCallerId\r\n");
        fputs($oSocket, "Exten: $number\r\n");
        fputs($oSocket, "Context: $strContext\r\n");
        fputs($oSocket, "Priority: $strPriority\r\n\r\n");
        fputs($oSocket, "Action: Logoff\r\n\r\n");
        sleep(2);
        $wrets = "";
        while (!feof($oSocket)) {
            $wrets.= fread($oSocket, 8192);
        }

        fclose($oSocket);

        $channel = substr(strstr($wrets, 'Channel:'), 8, 12); // HERE´S THE CATCH

    }

    return ($channel != "") ? trim($channel) : false;

}[/code]

Script to hang the call:

[code]private function hangup_AMI($channel) {

    $amiHost = "192.168.1.x"; 
    $amiUser = "user"; 
    $amiSecret = "secret"; 
    $amiChannel = $channel;  // this is the channel obtained with the above method someting like DAHDI/1-1

    $amiStatusExt = 0;

    if ($amiChannel == null) :
        exit();
    endif;

    $errno = 0;
    $errstr = 0;
    $oSocket = fsockopen($amiHost, 5038, $errno, $errstr, 20);
    if (!$oSocket) {
        echo "$errstr ($errno)<br>\n";
    } else {

        fputs($oSocket, "Action: login\r\n");
        fputs($oSocket, "Events: off\r\n");
        fputs($oSocket, "Username: $amiUser\r\n");
        fputs($oSocket, "Secret: $amiSecret\r\n\r\n");

        fputs($oSocket, "Action: Hangup\r\n");
        fputs($oSocket, "channel: $amiChannel\r\n\r\n");

        fputs($oSocket, "Action: Logoff\r\n\r\n");
        sleep(2);

        fclose($oSocket);

    }
}[/code]

I hope it is helpful to someone.
If anyone knows a better way to do that I’ll be very grateful!