I'm using pfsockopen to open persistent internet in php and using fread() to read the return value. Most of the times it works properly, but sometimes(1/100,000) fread() return empty string or return less than 5 bytes string, AND the Transfer-Encoding is chunked. This is the main code I use:
$s_pack = self::buildRequestBody($host, $uri, $headers, $body, $method);
$fp = pfsockopen($ip, $port, $i_err, $s_err, $timeout);
stream_set_timeout($fp, $timeout);
$write_status = fwrite($fp, $s_pack);
$status = stream_get_meta_data($fp);
$s_resp = '';
while (!feof($fp) && !$status['timed_out']) {
$buffer = fread($fp, 51200);
$s_resp .= $buffer;
$status = stream_get_meta_data($fp);
// read chunked body
// read content
}
var_dump(strlen($s_resp));
Sometimes the result print property, but sometimes strlen($s_resp) is equal 0, or 1, or 2, or 3... so, it cannot read the chunked body and content.