title: Fire-and-forget in PHP
body: <p><strong>Final update</strong></p>
  
  <p>Seems like I did make a very simple error. Since I already have a stream implementation I can just not start reading from the stream :D</p>
  
  <hr>
  
  <p>I'm trying to achieve <strong>fire-and-forget</strong> like functionality in PHP.</p>
  
  <blockquote>
    <p><a href="http://www.php.net/manual/en/features.connection-handling.php#93441" rel="nofollow">From php.net</a></p>
  </blockquote>
  
  <pre><code>&lt;?php
  ignore_user_abort(true);
  header("Content-Length: 4");
  header("Connection: Close");
  echo "abcd";
  flush();
  
  sleep(5);    
  echo "Text user should not see"; // because it should have terminated
  ?&gt;
  </code></pre>
  
  <p>This works if I open the script with a browser. (shows "abcd").
  But if I open it with <code>file_get_contents</code> or some <em>stream</em> library it will wait for ~5 seconds and show the second text as well.</p>
  
  <p>I'm using PHP 5.2.11 / Apache 2.0</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>I seems there is some confusion about what I'm trying to accomplish.</p>
  
  <p>I don't want to hide output using output buffers (that's stupid). I want to have the client terminate before the server starts a possibly lengthy process (<code>sleep(5)</code>) and I don't want the client to wait for it (this is what fire-and-forget means, sort off).
  The use of output buffers is merely a side effect. I've amended the sample code without the use of output buffers.</p>
  
  <p>What I don't understand is: why does this script behave differently when accessing it from the browser vs. fetching it in PHP with <code>file_get_contents("http://dev/test.php")</code> or some <em>stream</em> library? What I've seen in testing is that for instance <code>stream_get_contents</code> will actually block for 5 seconds before it returns any output at all, the is quite the opposite of what I want.</p>
  
  <p><strong>Update2</strong></p>
  
  <p>Some more results:</p>
  
  <ul>
  <li>The browser somehow responds to the <code>flush()</code>. I can't figure out how to replicate this behavior with <em>streams</em> in PHP, my streams keep blocking.</li>
  <li>I've tried <code>fread</code> and found that it behaves similar to <code>stream_get_contents</code>.</li>
  <li>Specifying a <code>maxlength</code> has no effect, it will still block for ~5 seconds.</li>
  <li>Changing the blocking mode has no effect (other than generating a bunch more calls <code>to stream_get_contents()</code>). It will wait ~5 seconds before returning anything.</li>
  <li><code>stream_set_read_buffer</code> has no effect (tested on a PHP 5.3.5 sever)</li>
  </ul>