Outgoing call working but voice communication not working

Hi,

I am Nitin (Sr. PHP Developer)

I have installed asterisk and freePBX on our local server and now we want call to our customer by asterisk and pbx by php api. I have implemented api for calling but Outgoing call working but voice communication not working please see below sample code.

Click2Dial <?php

strName = isset(_POST[‘txtname’])?$_POST[‘txtname’]:’’;
strExten = isset(_POST[‘txtphonenumber’])?$_POST[‘txtphonenumber’]:’’;
$callNumber = $strExten;

$strHost = “127.0.0.1”;
$strUser = “xxxx”;
$strSecret = “xxxxxxxxxxxxxxxxxxxxx”;
$strChannel = “local/$callNumber@from-internal”;
$strContext = “from-internal”;
$strWaitTime = “300”;
$strPriority = “1”;
$strMaxRetry = “2”;
#--------------------------------------------------------------------------------------------
#Shouldn’t need to edit anything below this point to make this script work
#--------------------------------------------------------------------------------------------
#specify the caller id for the call
$strCallerId = “Web-”.$strName . " <$callNumber>";
$length = strlen($strExten);
if ($length == 10 && is_numeric($strExten))
{
$oSocket = fsockopen($strHost, ‘5038’, $errnum, $errdesc) or die(“Connection to host failed”);
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: 1001\r\n”);
fputs($oSocket, “Context: $strContext\r\n”);
fputs($oSocket, “Priority: 1\r\n\r\n”);
fputs($oSocket, “Action: Logoff\r\n\r\n”);
sleep(3);
while ($wrets=fgets($oSocket)) {
var_dump($wrets);
}
fclose($oSocket);
?>

We are processing your call, please wait about two minutes. If you have not received our call within two minutes, verify that your name and number were entered correctly and try again.

Go Back

<?php } else { ?>

Enter your name and 10-digit number (e.g. 7875551234). If available, we will call you within the next two minutes. Name:   

Number:

<?php } ?>

Assuming that you are feeding your script with valid data, the way you retrieve the AMI answer is a bit week and may not capture everything. I’d suggest something like the following:

function get_response($sock) {
    $line = '';
    $response = '';
    while ($line != "\r\n") {
        $line = fgets($sock, 256);
        $response .= $line;
    }
    return $response;
}

The next thing are your 3 seconds at the wrong location. I prefer to originate calls with the option

Async: yes

and these problems are gone. You need to capture the answer from the originate and the logoff actions to be sure that Asterisk does thank you for all the fish.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.