- 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楼
fseek和ftell是两个在文件操作中常用的函数,它们的功能是使文件指针移动到文件的末尾,返回文件的当前位置。fseek用于向文件的当前位置移动指定的字节数,而ftell返回文件的当前位置。在PHP中,
fseek的语法如下:php fseek($file, $offset, $unit);其中,
$file是你要移动文件指针的文件对象,$offset是你要移动的字节数,$unit是你要移动的字节单位。ftell的语法如下:php ftell($file);但是,
fseek比ftell更慢,因为fseek需要将文件指针移动到文件的末尾,并且返回的位置不是文件的当前位置。如果你只是需要返回文件的当前位置,那么使用ftell就足够了。如果你经常遇到
fseek和ftell特别慢的情况,可能是因为你的文件非常大,需要移动的字节数也很大。在这种情况下,你可能需要考虑使用更高效的数据结构或算法,或者调整你的文件操作策略。
更多回答
网站公告
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
