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."; ?>