note 75063 added to function.fread

From: Date: Thu, 10 May 2007 14:52:55 +0000
Subject: note 75063 added to function.fread
Groups: php.notes 
Request: Send a blank email to php-notes+get-126389@lists.php.net to get a copy of this message
reading from a socket stream can be different to the behaviour expected, since you have not set stream_set_blocking to 1. sample source: $fp = fsockopen ($server, $port, $errno, $errstr, $socket_timeout); $header = ''; do { $header.=fread($fp,1); $i++; } while (!preg_match('/\\r\\n\\r\\n$/', $header) && $i < $maxheaderlenth); preg_match('/Content\\-Length:\\s+([0-9]*)\\r\\n/', $header,$matches); $buffer = fread($this->_fp, $matches[1]); if i.e. the content length is 50000 and the responding server is to slow (means 50000 are not completely sent when fread is called) you'll only receive the number of bytes sent by the responding server at the time fread is called. fread will not wait for any data to complete the given size. as described in user notes on stream_set_blocking there seems to be a bug using stream_set_blocking. a workaround - well, not the best way - is to read the response split to 1 byte instead of $buffer = fread($this->_fp, $matches[1]); you'd write $buffer = ''; for($i = 0; $i < $matches[1]; $i++){ $buffer .= fread($this->_fp, 1); } it several tests this seems like it works. ---- Server IP: 212.124.37.9 Probable Submitter: 213.209.121.3 ---- Manual Page -- http://www.php.net/manual/en/function.fread.php Edit -- https://master.php.net/note/edit/75063 Del: integrated -- https://master.php.net/note/delete/75063/integrated Del: useless -- https://master.php.net/note/delete/75063/useless Del: bad code -- https://master.php.net/note/delete/75063/bad+code Del: spam -- https://master.php.net/note/delete/75063/spam Del: non-english -- https://master.php.net/note/delete/75063/non-english Del: in docs -- https://master.php.net/note/delete/75063/in+docs Del: other reasons-- https://master.php.net/note/delete/75063 Reject -- https://master.php.net/note/reject/75063 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126389) next »