#28682 [Opn->Bgs]: fread appears to drop the buffer
ID: 28682
Updated by: iliaa@php.net
Reported By: ewade at vlender dot com
-Status: Open
+Status: Bogus
Bug Type: Sockets related
Operating System: GNU/Linux
PHP Version: 4.3.7
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
You should never rely on single fread() to read all
available data especially when dealing with remote servers.
Replace your code with:
$xml = '';
while (($line = fread($fp, 1000000)))) {
$xml .= $line;
}
Previous Comments:
------------------------------------------------------------------------
[2004-06-07 20:54:54] ewade at vlender dot com
Description:
------------
This function works fine with with PHP-4.3.1 but since recently
upgrading to 4.3.6 and/or 4.3.7 it strips out the headers as appopriate
but returns an empty body. (I've cut out the host code and the cookie
code for obvious security issues.)
What's weird is if I do not strip out the headers they list the correct
Content-Length for the body, but the body is empty.
At first I thought it might be the cURL library so I downgraded from
cURL-7.11.1 to cURL-7.10.3, but that did not resolve the issue. It
will only work with PHP-4.3.1 which seems rather odd. Any thoughts are
greatly appreciated in resolving this issue.
############## ./configure --with apxs=/usr/local/apache/bin/apxs
--enable-bcmath --with-curl --with-mysql=/usr/local/mysql
--with-mcrypt=/usr/local/lib --with-openssl=/usr/local/ssl
--without-pear --disable-cgi --with-gd --with-zlib
--with-jpeg-dir=/usr/local/lib --with-pfpro --enable-xml
Reproduce code:
---------------
function send_to_factual_data($post_data)
{
// Open an SSL connection to the server
$fp = fsockopen("ssl://priate.ssl.host", 443, $errno, $errstr);
// Make sure we're connected
if (!$fp)
return "Error Connecting To FactualData.com - $errstr
($errno)";
else
{
// Send the HTTP headers
fputs($fp, "POST /applet/path/here HTTP/1.0\r\n" .
"User-Agent: Mozilla/4.00\r\n" .
"Content-Type: text/xml\r\n" .
"Content-Length: " . strlen($post_data) . "\r\n" .
"Cookie: CERT=*CENSORED*;\r\n\r\n");
// Send the POST data
fputs($fp, $post_data);
// Read the response - limit of 1M bytes should be OK
$xml = @ fread($fp, 1000000);
// Length of server response including headers
$len = strlen($xml);
// Strip the headers
$xml = strstr($xml, "\r\n\r\n");
// Skip the 2 CRLFs if we removed the headers
if ($len != strlen($xml))
$xml = substr($xml, 4);
return $xml;
}
} // end of function: send_to_factual_data
Expected result:
----------------
When I run the code I expect to have an XML credit report file returned
to me. I expect the code to strip off the HTML headers and leave the
body intact for me to parse with my XML parsing scripts.
Instead what happens is I get back the HTML headers that say they have
content, but there is no content to be found.
Actual result:
--------------
HTTP/1.1 200 OK
Content-Type: text/xml
Content-Length: 57717
Connection: close
Date: Mon, 07 Jun 2004 18:51:24 GMT
Server: Apache Tomcat/4.1.27-LE-jdk14 (HTTP/1.1 Connector)
### The Content-Type and Content-Length are correct, but there is no
data to be found, just the headers.
Thank you for your time!
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=28682&edit=1
Thread (2 messages)