#21202 [Opn->Fbk]: fsockopen / fread fails unless exact length is given

From: Date: Thu, 26 Dec 2002 22:06:36 +0000
Subject: #21202 [Opn->Fbk]: fsockopen / fread fails unless exact length is given
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-28425@lists.php.net to get a copy of this message
ID: 21202 Updated by: derick@php.net Reported By: lastritesdf@yahoo.com -Status: Open +Status: Feedback Bug Type: Sockets related Operating System: Linux, 2.4.19 PHP Version: 4.2.3 New Comment: Please try using this CVS snapshot: http://snaps.php.net/php4-latest.tar.gz For Windows: http://snaps.php.net/win32/php4-win32-latest.zip Previous Comments: ------------------------------------------------------------------------ [2002-12-26 16:05:01] lastritesdf@yahoo.com Hi there... I wanted to fetch a webpage's contents. I used to be able to do so by fread'ing with a huge number as length: ////////////////// $fp=fsockopen( $host, $port, $errno, $errstr, 120 ); // assumed host and port were previously defined $headers = "GET /jornalmn/index.htm HTTP/1.0\r\n" ."User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n" ."Host: www.meionorte.com\r\n" ."Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*\r\n" ."Connection: Keep-Alive\r\n" ."Cache-Control: no-cache\r\n" ."\r\n"; fwrite($fp,$headers, strlen($headers)); $fetched_content = fread($fp,1000000000); fclose($fp); //////////////// Just recently this stopped working. Actually, I would eventually fetch the page, but only after the connection timed out. After a lot of debugging I found out I'd have sucess by specifying the EXACT file size to fread. I had to adapt my scripts and tell them to either: a) Capture the "Content-length" response value sent by the server (easy way); b) Fread little by little (128 bytes a time) and use socket_get_status($fp) everytime to check for EOF condition and the number of unread bytes. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=21202&edit=1

« previous php.bugs (#28425) next »