doufan3958 2017-04-19 10:45
浏览 155

PHP pfsockopen - fread()有时会返回一个空字符串或返回少于5个字节的字符串

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.

the one screenshot for error log:

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 我要一个分身加定位两个功能的安卓app
    • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
    • ¥15 IAR程序莫名变量多重定义
    • ¥15 (标签-UDP|关键词-client)
    • ¥15 关于库卡officelite无法与虚拟机通讯的问题
    • ¥15 目标检测项目无法读取视频
    • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
    • ¥100 求采集电商背景音乐的方法
    • ¥15 数学建模竞赛求指导帮助
    • ¥15 STM32控制MAX7219问题求解答