4

I'm trying to read the POST data from a request with "Transfer-Encoding: chunked" while it's being sent but can't get the script to fire until after all of the data has been received, is it possible to get PHP to be able to react to a chunked request as it's coming through?

Using PHP 5.3.8 with Apache.

nortron
  • 3,873
  • 1
  • 24
  • 27
  • 1
    This is a function of Apache, not PHP. And the short answer is "no". Whatever you are doing, you will probably have more success in something like [Node.js](http://nodejs.org/) – DaveRandom Apr 03 '12 at 10:29
  • @DaveRandom: Please post that as an answer. So you're saying that Apache holds up the request until it is complete before forwarding to PHP? Can I get the long answer? – nortron Apr 03 '12 at 17:45

1 Answers1

4

The current Apache API does not provide the ability to invoke a response handler before the request has been completely received. It is not possible for any module (mod_php, mod_cgi et al) to start processing and respond to the request until the handler phase, and he Apache core does not reach this phase until after the request has been completely received and preliminary processing/parsing checks have completed.

It is unusual to send a chunked stream as a request, normally something like this is a response. HTTP is a protocol designed primarily for one-to-many content delivery - an HTTP server is expecting to server content to many clients, rather than to have many clients deliver content to it. Normally to achieve this you would invoke a script on the server that will connect to the remote stream (be the requester).

What exactly are you trying to do? What content are you trying to deliver? Do you have control over both ends of the connection?

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • Great information, thank you. I'm trying to speed up delivery of recorded audio from a mobile phone to a speech recognition API, both of which I control. I'd like to have the speech recognition side begin receiving and processing data as soon as the microphone delivers it instead of waiting for the full recording to complete. Any recommendations? – nortron Apr 04 '12 at 02:26
  • 1
    Rather than sticking to a single HTTP request, maybe you could split the stream up in separate requests? You could, for example, cache a few KB of audio data on the phone, and send it to the server with e.g. a cookie value containing a unique ID for the call. Then the server can use this id to process the message in chunks, and put it back together again at the server side. This approach would also have the advantage of potentially being able to process more than one chunk at once on the server. It would be more complicated though - namely to keep track of multiple concurrent streams. – DaveRandom Apr 04 '12 at 09:50