续:fsockopen,读返回数据不易采用feof函数

换另一种方式来读数据:

<?php
function httpPost($host,$url,$data,$p)
{
$conn = fsockopen($host,80,$errno, $errstr, 30);
if (!$conn)
{
echo "$errstr ($errno)<br />\n";
return;
}

$header = "POST ".$url." HTTP/1.1\r\n";
$header.= "Host : {$host}\r\n";
$header.= "Content-type: application/x-www-form-urlencoded\r\n";
$header.= "Content-Length:".strlen($data)."\r\n";
$header.= "Connection: Keep-Alive\r\n\r\n";
$header.= "{$data}\r\n\r\n";

fwrite($conn,$header);


//读内容
$resp='';
$start = microtime(true);


/*
* echo "开始读<br>";
while (!feof($conn)) {
echo "读之前:".(microtime(true)-$start)."<br>";
$resp .= fread($conn,64);
echo "读之后:".(microtime(true)-$start)."<br>";
}
*/
$len=-1;

//开始读头,读到空行时,表明头已读完
echo "开始读head<br>";
while( ($line=trim(fgets($conn))) != "" )
{
echo "head读之前:".(microtime(true)-$start)."<br>";
$header.=$line;
if(strstr($line,"Content-Length:"))
{
list($cl,$len)=explode(" ",$line);
}
echo "head读之后:".(microtime(true)-$start)."<br>";
}

echo "开始读body:".(microtime(true)-$start)."<br>";
$body=fread($conn,$len);
echo "内容:".$body."<br>";
echo "读完关闭前:".(microtime(true)-$start)."<br>";
fclose($conn);
echo "关闭:".(microtime(true)-$start)."<br>";
return $resp;
}

httpPost("127.0.0.1","/aa.php","test",true);
?>

结果:

开始读head
head读之前:0.00082898139953613
head读之后:0.00084185600280762
head读之前:0.00084590911865234
head读之后:0.00084781646728516
head读之前:0.00085091590881348
head读之后:0.00085282325744629
head读之前:0.00085592269897461
head读之后:0.00085783004760742
head读之前:0.00085997581481934
head读之后:0.00086688995361328
head读之前:0.0008699893951416
head读之后:0.00087189674377441
head读之前:0.00087499618530273
head读之后:0.00087690353393555
head读之前:0.00088000297546387
head读之后:0.00088191032409668
开始读body:0.00088381767272949
内容:hello
读完关闭前:0.00088787078857422
关闭:0.0009307861328125

整个过程速度很快。

纵观两个函数及耗时,我觉得是feof判断结束有bug。所以推荐读SOCK的做法是,先取到协议头里的Content-Length,然后再根据Content-Length指定的长度读内容。

欢迎讨论。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值