socket
来源:互联网 发布:excel导入oracle数据库 编辑:程序博客网 时间:2024/05/16 10:35
<?phpinterface Proto{ function post(); function get(); function conn($url); function close();}class Http implements Proto{ protected $version= 'HTTP/1.1'; protected $line = array(); protected $header = array(); protected $body = array(); protected $url = array(); protected $fh = null; protected $errorno = -1; protected $errorstr=''; protected $response=''; const CRLF = "\r\n"; public function __construct($url){ $this->conn($url); $this->setHeader('Host: '.$this->url['host']); } //请求行 protected function setLine($method){ $this->line[0] = $method.' '.$this->url['path'].' '.$this->version; } //头信息 protected function setHeader($headerline){ $this->header[] = $headerline; } //主体 protected function setBody(){} //连接URL public function conn($url){ $this->url = parse_url($url); //判断端口 if(!isset($this->url['port'])){ $this->url['port'] = 80; } $this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errorno,$this->errorstr,3 ); } //构造get查询 public function get(){ $this->setLine('GET'); $this->request(); return $this->response; } //构造POST查询 public function post(){ $this->setLine('POST'); } //请求 public function request(){ $req = array_merge($this->line,$this->header,array(''),$this->body,array('')); $req = implode(self::CRLF,$req); fwrite($this->fh,$req); while(!feof($this->fh)){ $this->response .= fread($this->fh,1024); } $this->close(); } //close public function close(){ }}$url = 'http://localhost/index.php';$http = new Http($url);echo $http->get();
post
<?phpinterface Proto{ function post(); function get(); function conn($url); function close();}class Http implements Proto{ protected $version= 'HTTP/1.1'; protected $line = array(); protected $header = array(); protected $body = array(); protected $url = array(); protected $fh = null; protected $errorno = -1; protected $errorstr=''; protected $response=''; const CRLF = "\r\n"; public function __construct($url){ $this->conn($url); $this->setHeader('Host: '.$this->url['host']); } //请求行 protected function setLine($method){ $this->line[0] = $method.' '.$this->url['path'].' '.$this->version; } //头信息 protected function setHeader($headerline){ $this->header[] = $headerline; } //主体 protected function setBody($body){ $this->body[] = http_build_query($body); } //连接URL public function conn($url){ $this->url = parse_url($url); //判断端口 if(!isset($this->url['port'])){ $this->url['port'] = 80; } $this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errorno,$this->errorstr,3 ); } //构造get查询 public function get(){ $this->setLine('GET'); $this->request(); return $this->response; } //构造POST查询 public function post($body = array()){ $this->setLine('POST'); $this->setBody($body); $this->setHeader('Content-type: application/x-www-form-urlencoded'); $this->setHeader('Content-length: '.strlen($this->body[0])); $this->request(); } //请求 public function request(){ $req = array_merge($this->line,$this->header,array(''),$this->body,array('')); $req = implode(self::CRLF,$req); fwrite($this->fh,$req); while(!feof($this->fh)){ $this->response .= fread($this->fh,1024); } $this->close(); } //close public function close(){ }}$url = 'http://localhost/index.php';$http = new Http($url);echo $http->post();
0 0
- socket
- socket
- Socket
- Socket
- Socket
- Socket
- Socket
- Socket
- socket
- Socket
- Socket
- Socket
- Socket
- socket
- socket
- socket
- socket
- socket
- java并发-Condition与Object.wait()、Object.notify()
- Google Cardboard 虚拟现实眼镜开发初步(一)
- 在hadoop中传递变量
- USB驱动开发之USB四种传输模式
- Android 动画
- socket
- linux字符编码转换
- 【MD5网站安全加固】
- Linux History记录加时间戳实用技巧
- Hadoop使用MultipleOutputs输出多文件或者指定命名
- web事件
- nrf51822 --- 模拟比较器
- Android Studio Unable to find instrumentation info
- 【MongoDB-query查询条件】