note 55787 added to function.fsockopen
| From: | l0gicatl0gicdotnet at osu1 dot php dot net | Date: | Sat, 13 Aug 2005 06:54:09 +0000 |
| Subject: | note 55787 added to function.fsockopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93533@lists.php.net to get a copy of this message | ||
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;
}
}
----
Manual Page -- http://www.php.net/manual/en/function.fsockopen.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55787
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55787&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55787&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55787&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55787&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55787&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55787&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55787&report=yes
Search -- http://master.php.net/manage/user-notes.php