note 96146 added to function.fsockopen

From: Date: Thu, 11 Feb 2010 01:50:57 +0000
Subject: note 96146 added to function.fsockopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-165854@lists.php.net to get a copy of this message
I wasn’t too happy with all the code snippets I found for unchunking chunked HTTP 1.1 replies, as most of them were either bloated with stuff I don’t need or calling several other functions and thus tricky to implement. And some didn’t even work at all for me... I just thought I could try implementing my own function and here it is. I haven't had any problems with it, so I hope someone finds this useful. <?php function unchunkHttp11($data) { $fp = 0; $outData = ""; while ($fp < strlen($data)) { $rawnum = substr($data, $fp, strpos(substr($data, $fp), "\r\n") + 2); $num = hexdec(trim($rawnum)); $fp += strlen($rawnum); $chunk = substr($data, $fp, $num); $outData .= $chunk; $fp += strlen($chunk); } return $outData; } ?> Before calling this function you may want to check if the content is really chunked using a code snipped like this: <?php while (!feof($fp)) { $reply .= fgets($fp, 1024); } $data = substr($reply, (strpos($reply, "\r\n\r\n")+4)); if (strpos(strtolower($reply), "transfer-encoding: chunked") !== FALSE) { $data = unchunkHttp11($data); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 85.176.130.114 ---- Manual Page -- http://www.php.net/manual/en/function.fsockopen.php Edit -- https://master.php.net/note/edit/96146 Del: integrated -- https://master.php.net/note/delete/96146/integrated Del: useless -- https://master.php.net/note/delete/96146/useless Del: bad code -- https://master.php.net/note/delete/96146/bad+code Del: spam -- https://master.php.net/note/delete/96146/spam Del: non-english -- https://master.php.net/note/delete/96146/non-english Del: in docs -- https://master.php.net/note/delete/96146/in+docs Del: other reasons-- https://master.php.net/note/delete/96146 Reject -- https://master.php.net/note/reject/96146 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165854) next »