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() );
慕丝7291255