I've observed the same behavior. This is what happens when you close the socket before logoff is completed. You need to wait until the logoff action is completed before closing the socket (your fclose() call). Alternately, use a proxy or a daemon that will sit between your script and the AMI and keep the socket open.<div class="gmail_extra">
<br><br><div class="gmail_quote">On Wed, Dec 12, 2012 at 12:44 PM, Faheem <span dir="ltr"><<a href="mailto:faheem_imt@yahoo.com" target="_blank">faheem_imt@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div style="font-size:12pt;font-family:arial,helvetica,sans-serif"><div style="font-family:arial,helvetica,sans-serif;font-size:12pt"><span>Hi,</span></div><div style="font-style:normal;font-size:16px;background-color:transparent;font-family:arial,helvetica,sans-serif">
<span>I'm getting errors while originating a call through AMI.</span></div><div style="background-color:transparent"><div style="font-style:normal;font-size:16px;background-color:transparent;font-family:arial,helvetica,sans-serif">
[Dec 12 21:18:35] ERROR[8661]: utils.c:1236 ast_careful_fwrite: fwrite() returned error: Broken pipe</div><div style="font-style:normal;font-size:16px;background-color:transparent;font-family:arial,helvetica,sans-serif">[Dec 12 21:18:35] ERROR[8661]: utils.c:1236 ast_careful_fwrite: fwrite() returned error: Broken
pipe</div><div style="font-style:normal;font-size:16px;background-color:transparent;font-family:arial,helvetica,sans-serif">[Dec 12 21:18:35] ERROR[8661]: utils.c:1236 ast_careful_fwrite: fwrite() returned error: Broken pipe</div>
<div style="font-style:normal;font-size:16px;font-family:arial,helvetica,sans-serif">Asterisk version 11.0.1</div><div style="font-style:normal;font-size:16px;font-family:arial,helvetica,sans-serif">OS: <span style="background-color:transparent">CentOS release 5.8 (Final)</span></div>
<div style="font-style:normal;font-size:16px;font-family:arial,helvetica,sans-serif"><br></div><div style="font-style:normal;font-size:16px;font-family:arial,helvetica,sans-serif"><span style="background-color:transparent">//manager.conf settings</span><br>
</div><div><div><span style="font-size:13px">[faheem]</span></div><div><span style="font-size:small">secret =f@xxxxxx</span></div><div><span style="font-size:small">permit=<a href="http://127.0.0.1/255.255.255.255" target="_blank">127.0.0.1/255.255.255.255</a></span></div>
<div><span style="font-size:small">read = system,call,log,verbose,agent,user,config,dtmf,reporting,cdr,dialplan</span></div><div><span style="font-size:small">write = system,call,agent,user,config,command,reporting,originate</span></div>
<div style="font-style:normal;font-size:16px;font-family:arial,helvetica,sans-serif"><br></div></div><div style="font-style:normal;font-size:16px;font-family:arial,helvetica,sans-serif"><span style="background-color:transparent;font-size:13px">///AMI script</span><br>
</div></div><div style="background-color:transparent"><div style="background-color:transparent"><span style="font-size:small"><?php</span></div><div style="background-color:transparent"><span style="font-size:small"><br>
</span></div><div style="background-color:transparent"><span style="font-size:small">$sys_ip = "127.0.0.1";</span></div><div style="background-color:transparent"><span style="font-size:small">$User_str = "faheem";</span></div>
<div style="background-color:transparent"><span style="font-size:13px">$Secret_str = "f@h33m112xxxxxx";</span></div><div style="background-color:transparent"><span style="font-size:small">$phoneNumb = 1234;</span></div>
<div style="background-color:transparent"><span style="font-size:small">$dialNumb = 4567;</span></div><div style="background-color:transparent"><span style="font-size:small">$spoofNumb = 786;</span></div><div style="background-color:transparent">
<span style="font-size:13px">$context = "xxxxx-xxxxx";</span></div><div style="background-color:transparent"><span style="font-size:small"><br></span></div><div style="background-color:transparent"><span style="font-size:small">$oSocket = fsockopen($sys_ip, 5038, $errnum, $errdesc) or die("Connection to host failed");</span></div>
<div style="background-color:transparent"><span style="font-size:13px;background-color:transparent">fputs($oSocket, "Action: login\r\n");</span><br></div><div style="background-color:transparent"><span style="font-size:13px">fputs($oSocket, "Username: $User_str\r\n");</span></div>
<div style="background-color:transparent"><span style="font-size:small">fputs($oSocket, "Secret: $Secret_str\r\n\r\n");</span></div><div style="background-color:transparent"><span style="font-size:small">fputs($oSocket, "Events: off\r\n\r\n");</span></div>
<div style="background-color:transparent"><span style="font-size:small">fputs($oSocket, "Action: originate\r\n");</span></div><div style="background-color:transparent"><span style="font-size:small">fputs($oSocket, "Channel: SIP/testTrunk/$phoneNumb\r\n");</span></div>
<div style="background-color:transparent"><span style="font-size:small">fputs($oSocket, "Exten: $dialNumb\r\n");</span></div><div style="background-color:transparent"><span style="font-size:small">fputs($oSocket, "Context: $context\r\n");</span></div>
<div style="background-color:transparent"><span style="font-size:small">fputs($oSocket, "Priority: 1\r\n\r\n");</span></div><div style="background-color:transparent"><span style="font-size:small">fputs($oSocket, "Timeout: 10000\r\n");</span></div>
<div style="background-color:transparent"><span style="font-size:13px">fputs($oSocket, "CallerId: $spoofNumb\r\n");</span></div><div style="background-color:transparent"><span style="font-size:small">fputs($oSocket, "Async: false\r\n");</span></div>
<div style="background-color:transparent"><span style="font-size:small">fputs($oSocket, "Action: Logoff\r\n\r\n");</span></div><div style="background-color:transparent"><span style="font-size:small">echo "originate executed";</span></div>
<div style="background-color:transparent"><span style="font-size:small">fclose($oSocket);</span></div><div style="background-color:transparent"><span style="font-size:small"><br></span></div><div style="background-color:transparent">
<span style="font-size:small">?></span></div><div style="font-style:normal;font-size:16px;font-family:arial,helvetica,sans-serif"><br></div></div><div style="font-style:normal;font-size:16px;background-color:transparent;font-family:arial,helvetica,sans-serif">
<br></div><div style="font-style:normal;font-size:16px;background-color:transparent;font-family:arial,helvetica,sans-serif"><div>Can any one please help me over it.</div><div>Thank you!</div><div><span style="font-size:12pt"> </span><br>
</div></div><div style="font-family:arial,helvetica,sans-serif;font-size:12pt"><div>Muhammad Faheem</div> <div><br><br></div></div></div></div><br>--<br>
_____________________________________________________________________<br>
-- Bandwidth and Colocation Provided by <a href="http://www.api-digital.com" target="_blank">http://www.api-digital.com</a> --<br>
New to Asterisk? Join us for a live introductory webinar every Thurs:<br>
<a href="http://www.asterisk.org/hello" target="_blank">http://www.asterisk.org/hello</a><br>
<br>
asterisk-users mailing list<br>
To UNSUBSCRIBE or update options visit:<br>
<a href="http://lists.digium.com/mailman/listinfo/asterisk-users" target="_blank">http://lists.digium.com/mailman/listinfo/asterisk-users</a><br></blockquote></div><br><br clear="all"><div><br></div>-- <br>-Chris Harrington<br>
<div>ACSDi Office: 763.559.5800</div><div><div>Mobile Phone: 612.326.4248</div></div><div><br></div><br>
</div>