note 55787 deleted from function.fsockopen by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 19:43:29 +0000 |
| Subject: | note 55787 deleted from function.fsockopen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142308@lists.php.net to get a copy of this message | ||
Note Submitter: l0gic at l0gic dot net
----
After a little experimenting, I worked out how to reliably handle chunked HTTP/1.1 data. The
following code assumes a connection has already been opened as $fp, and the response header has been
read into the array $header. A loop is used to read data until the entire chunk length has been
read, due to fsockopen()'s tendency to stop reading when a new packet is received. Please note
that I have not put this code through thorough testing; this is merely to demonstrate the
methodology.
if (isset($header['Transfer-Encoding'])) {
// Read the length of the current chunk
while ($chunk_length = hexdec(fgets($fp))) {
// Initialize counter and buffer
$read_length = 0;
$data = NULL;
// Read $chunk_length bytes of data
while ($read_length < $chunk_length) {
$data .= fread($fp, $chunk_length - $read_length);
$read_length = strlen($data);
}
// Output the data read
echo $data;
}
}