Re: handling chunked input from php://stdin

From: Date: Wed, 13 May 2009 11:09:40 +0000
Subject: Re: handling chunked input from php://stdin
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-292522@lists.php.net to get a copy of this message
Shawn McKenzie wrote:
whisperstream wrote:
I have a server running that receives xml formatted events from other services I have no control over. For certain events the transfer-encoding is chunked. I was just doing $input = file_get_contents('php://stdin'); and this works well until there is chunked input. Then I tried $handle = fopen('php://input', "rb"); $input = ''; while (!feof($handle)) { $input .= fread($handle, 8192); } fclose($handle); And that gives about the same result, has anyone else come across this and how did they solve it? Thanks in advance
There aren't really many examples around, but check http_chunked_decode() from PECL.
simples! function HTTPChunkDecoder( $chunkedData ) { $decodedData = ''; do {
    $tempChunk = explode(chr(13).chr(10), $chunkedData, 2);
    $chunkSize = hexdec($tempChunk[0]);
    $decodedData .= substr($tempChunk[1], 0, $chunkSize);
    $chunkedData = substr($tempChunk[1], $chunkSize+2);
} while (strlen($chunkedData) > 0); return $decodedData; }

« previous php.general (#292522) next »