为了账号安全,请及时绑定邮箱和手机立即绑定

php feof 特别慢

php feof 特别慢

PHP
慕妹3242003 2019-03-05 22:22:19
class Socket { protected $crlf = "\r\n"; protected $host = ''; protected $port = 80; protected $method = 'GET'; protected $path = '/'; protected $httpVersion = 'HTTP/1.1'; protected $headers = array(); protected $body = ''; protected $error = array(); protected $timeout = 5; public function url($url) { $info = parse_url($url); $this->host = $info['host']; isset($info['path']) && $this->path = $info['path']; isset($info['port']) && $this->port = $info['port']; return $this; } public function method($method) { $this->method = $method; return $this; } public function path($path) { $this->path = $path; return $this; } public function httpVersion($version) { $this->httpVersion = $version; return $this; } public function host($host) { $this->host = $host; return $this; } public function header($header) { $this->headers[] = $header; return $this; } public function body($body) { $this->body = $body; return $this; } public function send() { $handle = fsockopen($this->host, $this->port, $this->error['errno'], $this->error['errstr'], $this->timeout); $req = join($this->crlf, array_merge( array("{$this->method} {$this->path} {$this->httpVersion}"), array("Host: {$this->host}"), $this->headers, array(''), array($this->body), array('') )); fwrite($handle, $req); $res = ''; while ( !feof($handle) ) { $res .= fread($handle, 1024); } fclose($handle); return $res; } } $s = new Socket(); var_dump( $s->url('http://baidu.com')->send() ); send()方法里的while特别慢,如果只是调用fread($handle, 1024),速度很快,朋友们看看什么情况啊?
查看完整描述

1 回答

?
慕丝7291255

TA贡献1859条经验 获得超6个赞

这个!feof($handle) 条件 一直成立 所以一直在循环 所以慢,你单独调用fread($handle, 1024) 只执行一次当然快

查看完整回答
反对 回复 2019-03-18
  • 1 回答
  • 0 关注
  • 424 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信