note 37569 deleted from function.fsockopen by pollita

From: Date: Thu, 20 Nov 2003 00:38:56 +0000
Subject: note 37569 deleted from function.fsockopen by pollita
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-60651@lists.php.net to get a copy of this message
Note Submitter: bart@mediawave.nl ---- HTTP/1.1 GET example with support for chunked transfer-encoding. <?php $path = '/index.php'; $host = 'www.php.net'; $CRLF = chr(13).chr(10); // Carrige Return and Line Feed $CRLF_SIZE = strlen($CRLF); $readlength = 1024; // default readlength $request = 'GET '.$path.' HTTP/1.1'.$CRLF; $request .= 'host:'.$host.$CRLF; $request .= 'content-type:text/html; charset=ISO-8859-4'.$CRLF; $request .= 'accept:text/*;q=0.5, text/html;q=1, */*;q=0'.$CRLF; $request .= 'accept-encoding:identity'.$CRLF; $request .= 'connection:close'.$CRLF; $request .= $CRLF; if ($pointer = fsockopen($host, 80, $errno, $errstr, 30)): fwrite($pointer, $request); // Read response header line by line. End of header is marked with an empty line. while ($header = rtrim(fgets($pointer, $readlength))): list($headerName, $headerValue) = explode (':', $header, 2); if ((strtolower(trim($headerName)) == 'transfer-encoding') && (strtolower(trim($headerValue)) == 'chunked')) $chunked = true; endwhile; endif; while (!feof($pointer)): if ($chunked): $chunksize = hexdec(rtrim(fgets($pointer, $readlength))); // Read a single line containing the chunksize if ($chunksize > 0): $data = fread($pointer, $chunksize); // Read the chunk while (strlen($data) < $chunksize): // Reading can stop halfway a chunk when a packet becomes available. $data .= fread($pointer, $chunksize - strlen($data)); // Read the rest of the chunk. endwhile; fread($pointer, $CRLF); // Read trailing carrige return $response .= $data; endif; else: // Normal reading mode $response .= fread($pointer, $readlength); endif; endwhile; echo $response; ?>

« previous php.notes (#60651) next »