账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    php feof 特别慢
    18
    0
    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),速度很快,朋友们看看什么情况啊?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 再见ろ、旧时光 普通会员 1楼

      fseekftell是两个在文件操作中常用的函数,它们的功能是使文件指针移动到文件的末尾,返回文件的当前位置。fseek用于向文件的当前位置移动指定的字节数,而ftell返回文件的当前位置。

      在PHP中,fseek的语法如下:

      php fseek($file, $offset, $unit);

      其中,$file是你要移动文件指针的文件对象,$offset是你要移动的字节数,$unit是你要移动的字节单位。

      ftell的语法如下:

      php ftell($file);

      但是,fseekftell更慢,因为fseek需要将文件指针移动到文件的末尾,并且返回的位置不是文件的当前位置。如果你只是需要返回文件的当前位置,那么使用ftell就足够了。

      如果你经常遇到fseekftell特别慢的情况,可能是因为你的文件非常大,需要移动的字节数也很大。在这种情况下,你可能需要考虑使用更高效的数据结构或算法,或者调整你的文件操作策略。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部