Facing error "fwrite () returned error :broken pipe" when we send multiple originate request to web based AMI

Hi everyone, i am facing error "fwrite () returned error :broken pipe" when we send multiple originate request to web based AMI. i am using below mention source code, so please guide me what is wrong with me .

AMI.txt (3.8 KB)

<?php $Mob=$_GET['Phoneno']; $Cid=$_GET['Clid']; $Pfile=$_GET['Playfile']; $Acode=$_GET['ACCcode']; $Chnl=$_GET['Channel']; $FinalMobile; if(!empty($Mob)) { $FinalMobile = ''.$_GET['Phoneno']; echo "$FinalMobile"; } else { echo "Mobno is not Availabe.
"; } if(!empty($Cid)) { $Cid=$_GET['Clid']; echo "
".$Cid; } else { echo "Clid is Blank"; } if(!empty($Pfile)) { $Pfile=$_GET['Playfile']; echo "
".$Pfile; } else { echo "Playfile is Blank"; } if(!empty($Acode)) { $Acode=$_GET['ACCcode']; echo "
".$Acode; } else { echo "ACCcode is Blank"; } if(!empty($Chnl)) { $Chnl=$_GET['Channel']; echo "
".$Chnl; } else { echo "Channel is Blank"; } $port = 5038; $username = "asteriskami"; $password = "\$12345"; $context = "testAMI"; $socket = stream_socket_client("tcp://localhost:5038"); if($socket) { echo "Connected to socket, sending authentication request.\n"; // Prepare authentication request $authenticationRequest = "Action: Login\r\n"; $authenticationRequest .= "Username: asteriskami\r\n"; $authenticationRequest .= "Secret: \$12345\r\n"; $authenticationRequest .= "Events: on\r\n\r\n"; // Send authentication request $authenticate = stream_socket_sendto($socket, $authenticationRequest); if($authenticate > 0) { // Wait for server response usleep(200000); // Read server response $authenticateResponse = fread($socket, 4096); // Check if authentication was successful if(strpos($authenticateResponse, 'Success') !== false) { echo "Authenticated to Asterisk Manager Inteface. Initiating call.\n"; // Prepare originate request $originateRequest = "Action: Originate\r\n"; $originateRequest .= "Channel: $Chnl/$FinalMobile\r\n"; $originateRequest .= "Callerid: $Cid\r\n"; $originateRequest .= "Application: Playback\r\n"; $originateRequest .= "Data: /usr/local/$Pfile\r\n"; $originateRequest .= "Context: testAMI\r\n"; $originateRequest .= "Exten: 1005\r\n"; fputs($socket, "Account: $Acode\r\n" ); $originateRequest .= "Priority: 2\r\n"; $originateRequest .= "Async: yes\r\n\r\n"; $originate = stream_socket_sendto($socket, $originateRequest); } if($originate > 0) { // Wait for server response usleep(1000); $originateResponse = fread($socket, 4096); echo $originateResponse . "\n"; $responce=fgets($socket,4096); socket_close($sock); echo $responce."\n"; // Check if originate was successful if(strpos($originateResponse, 'Success') !== false) { echo "Call initiated, dialing.\n"; } else { echo "Could not initiate call.\n"; } } else { echo "Could not write call initiation request to socket.\n"; } } else { echo "Could not authenticate to Asterisk Manager Interface.\n"; } } else { echo "Could not write authentication request to socket.\n"; } echo "Unable to connect to socket."; ?>

That error it is harmless, it is caused most of the time by missing acknowledgement of the logout request

sir one more thing is that i want to get the response or store the response of every call of using above code please guide me how can do that.

This is not an error, not an issue, not something you need to be concerned about.

fwrite () returned error :broken pipe

It’s typically caused when the connected party hangs up during Playback(). The channel is gone, the user disconnected, a broken pipe, and there is nowhere for fwrite() to send the audio to.

same => n,Playback(long_announcement) ; 2 minutes long

If the connected party disconnects before long_announcement completes, you will get the fwrite() log entry.


If you want help with some code, please consider posting in markdown format.

01: ```
02: ... your code here ...
03: ```

Your code it it is impossible to read, I cant help and also for that new question open a new thread we a readable code, and quick note your new question seems to be a php question more than Asterisk

AMI.txt (3.8 KB)

this is my code sir please guide me for getting response of every originate call from this code

U can use local channel to post back DIALSTATUS variable to your script