conn($url); $this->setHeader("Host: ".$this->url['host']); } // 连接 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->errno,$this->errstr,$this->expires_time); } // 设置请求行 protected function setLine($method) { $this->url['path'] .= isset($this->url['query']) ? '?'.$this->url['query'] : ''; $this->line[] = $method.' '.$this->url['path'].' '.$this->version; } // 设置头信息 public function setHeader($header) { $this->header[] = $header; } // 设置实体信息 protected function setBody($data) { $this->body[] = http_build_query($data); // $this->body[0] = $data; } // 发送get请求 public function get() { $this->setLine('GET'); $this->request(); return $this->response; } // 发送post查询 public function post($data) { $this->setLine('POST'); $this->setHeader("Content-type: application/x-www-form-urlencoded"); $this->setBody($data); $this->setHeader("Content-length: ".strlen($this->body[0])); $this->request(); return $this->response; } // 真正的请求 protected function request() { $req = array_merge($this->line,$this->header,array(''),$this->body,array('')); $req = implode(self::CRLF,$req); echo $req; // exit; // 写入 fwrite($this->fh,$req); while(!feof($this->fh)){ $this->response .= fread($this->fh,1024); } $this->close(); } // 关闭连接 public function close() { fclose($this->fh); } }// $url = 'http://shunping.com/msg.php';// $url = 'http://news.163.com/13/0613/09/9187CJ4C00014JB6.html';// $http = new Http($url);// echo $http->get();// tit=test&con=tset&submit=%E6%8F%90%E4%BA%A4// $data['tit'] = 'test';// $data['con'] = 'tset';// $data['submit'] = '%E6%8F%90%E4%BA%A4'; // $data['submit'] = '提交';// echo $http->post($data);?>