Issues in Apache and Asterisk [ Connection reset by Peer ] when using Asterisk AMI Originate Call in PHP

Hello Team,

Greeting Day to All,

Many thanks to all my community Members through which I can solve any problem in less time.
Today I’m facing one problem which is related to OBD (Outbound Dialling) via AMI Originate in PHP
Below I’m sharing AMI Originate code which is written in PHP through this code my OBD is working but this code is overheating Apache, each AMI request increasing Apache process thread even after the call being disconnected. Ideally after disconnect Apache request has been destroyed but not happen in this case .
I think there is infinite loop is running on while loop, if I remove while statement from here then getting Connection Reset by Peer ERROR in Asterisk console.
If not then Apache Process hit increases on each request and causing Apache in Ideal
Please help on this.

If possible please suggest How to optimize Server version: Apache/2.4.6 (CentOS)

Please share some optimization script for Apache, when I hit 200 or 300 concurrent calls using AMI PHP after that Apache is not working and I have to restart apache in that case then it will work.

while(!feof($oSocket)) {
$wrets .= fread($socket, 4096);
echo $i++;
}

Below I’m sharing PHP Code :

<?php
 if ( ! empty( $calling_number ) ){
               $timeout = 10;
                $socket = fsockopen($asterisk_ip,"5038", $errno, $errstr, $timeout);
                fputs($socket, "Action: Login\r\n");
                fputs($socket, "UserName: $user\r\n");
                fputs($socket, "Secret: $pass\r\n\r\n");
                //$wrets=fgets($socket,128);
                //fclose($socket);
                fputs($socket, "Action: Originate\r\n" );
                fputs($socket, "Channel: Local/$calling_number@outbound-ami-status/n\r\n" );
                fputs($socket, "Exten: $calling_number\r\n" );
                fputs($socket, "Context: outbound-test\r\n" );
                fputs($socket, "Priority: 1\r\n" );
                fputs($socket, "Callerid: XXXXXXX\r\n" );
                fputs($socket, "WaitTime: 5\r\n" );
                fputs($socket, "Variable: web_lead_id=$id\r\n");
		fputs($socket, "Variable: device_type=$devicetype\r\n");
                fputs($socket, "Async: true\r\n" );
                fputs($socket, "Action: logoff\r\n\r\n" );
               // $wrets .= fread($socket, 128);
               // $i=0;
              // $wrets .= fread($socket, 128);
	       while(!feof($oSocket)) {
		     $wrets .= fread($socket, 4096); // OR $wrets .= fread($socket, 128);
		     echo $i++;
		 }
		echo "ASTERISK MANAGER OUTPUT:$wrets";
                //$wrets=fgets($socket,128);
                //echo $wrets;
                //fclose($socket);
                //echo <<<ASTERISKMANAGEREND ASTERISK MANAGER OUTPUT:   $wrets ASTERISKMANAGEREND;
        }
        else{
                echo "Unable to determine number from (" . $_REQUEST['calling_number'] . ")\r\n";
        }
?>

Awaiting you response

As you scale, you might want to look into PHP socket_select function, as well as fclose function (which you have commented out.)

Also do you actually see any output from your echo $i++; line ?

Fix the variable name on the while $oSocket, this is posibly cuasing an infinitive loop

1 Like

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