php 利用socket发送http请求(get,post),socketget今天给大家带来的是如何利用socket发送get,post请求。我借用燕十八老师封装好的一个http类给进行说明。
在日常编程中相信很多人和我一样大部分时间是利用浏览器向服务器提出get,post请求,那么可否利用其它方式提出get,post请求呢?答案必然是肯定的。了解过http协议的人知道,浏览器提交请求的实质是向服务器发送一个请求信息,这个请求信息有请求行,请求头,请求体(非必须)构成。服务器根据请求信息返回一个响应信息。连接断开。
http请求的格式如下所示:
[]
http响应的格式与请求的格式十分相似:
[]
我们可以利用http发送请求的原理,可以重新考虑利用socket发送http请求。
socket的英文原义是“孔”或“插座”。通常也称作“套接字”,用于描述ip地址和端口,是一个通信链的句柄,可以用来实现不同虚拟机或不同计算机之间的通信。在internet上的主机一般运行了多个服务软件,同时提供几种服务。每种服务都打开一个socket,并绑定到一个端口上,不同的端口对应于不同的服务。如此看来,其实利用socket操作远程文件和读写本地的文件一样容易,把本地文件看成通过硬件传输,远程文件通过网线传输就行了。
因而可以将发送请求的考虑成 建立连接->打开socket接口(fsockopen())->写入请求(fwrite())->读出响应(fread()->关闭文件(fclose())。话不多说,直接上代码:
conn($url); $this->setheader('host: ' . $this->url['host']); } // 此方法负责写请求行 protected function setline($method) { $this->line[0] = $method . ' ' . $this->url['path'] . '?' .$this->url['query'] . ' '. $this->version; } // 此方法负责写头信息 public 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; } // 判断query if(!isset($this->url['query'])) { $this->url['query'] = ''; } $this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3); } //构造get请求的数据 public function get() { $this->setline('get'); $this->request(); return $this->response; } // 构造post查询的数据 public function post($body = array()) { $this->setline('post'); // 设计content-type $this->setheader('content-type: application/x-www-form-urlencoded'); // 设计主体信息,比get不一样的地方 $this->setbody($body); // 计算content-length $this->setheader('content-length: ' . strlen($this->body[0])); $this->request(); return $this->response; } // 真正请求 public function request() { // 把请求行,头信息,实体信息 放在一个数组里,便于拼接 $req = array_merge($this->line,$this->header,array(''),$this->body,array('')); //print_r($req); $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); }}
利用此类发送一个简单的get请求:
get();print_r($response);
返回值为信息,可以对响应信息进行进一步处理,得到自己想得到的内容。
我们来看下一个具体的实例
parseurl($url); $this->header['host'] = $this->urlinfo['host']; return $this; } public function get($header = array()) { $this->header = array_merge($this->header, $header); return $this->request('get'); } public function post($header = array(), $body = array()) { $this->header = array_merge($this->header, $header); if ( !empty($body) ) { $this->body = http_build_query($body); $this->header['content-type'] = 'application/x-www-form-urlencoded'; $this->header['content-length'] = strlen($this->body); } return $this->request('post'); } private function request($method) { $header = ; $this->requestline = $method.' '.$this->urlinfo['path'].'?'.$this->urlinfo['query'].' '.$this->protocol; foreach ( $this->header as $key => $value ) { $header .= $header == ? $key.':'.$value : $this->sp.$key.':'.$value; } $this->requestheader = $header.$this->sp.$this->sp; $this->requestinfo = $this->requestline.$this->sp.$this->requestheader; if ( $this->body != ) { $this->requestinfo .= $this->body; } /* * 注意:这里的fsockopen中的url参数形式为www.xxx.com * 不能够带http://这种 */ $port = isset($this->urlinfo['port']) ? isset($this->urlinfo['port']) : '80'; $this->fp = fsockopen($this->urlinfo['host'], $port, $errno, $errstr); if ( !$this->fp ) { echo $errstr.'('.$errno.')'; return false; } if ( fwrite($this->fp, $this->requestinfo) ) { $str = ; while ( !feof($this->fp) ) { $str .= fread($this->fp, 1024); } $this->responseinfo = $str; } fclose($this->fp); return $this->responseinfo; } private function parseurl($url) { $this->urlinfo = parse_url($url); }}// $url = http://news.163.com/14/1102/01/aa0pfa7q00014aed.html;$url = http://localhost/httppro/post.php;$http = http::create()->init($url);/* 发送get请求 echo $http->get(array( 'user-agent' => 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/35.0.1916.153 safari/537.36',));*/ /* 发送post请求 */echo $http->post(array( 'user-agent' => 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/35.0.1916.153 safari/537.36',), array('username'=>'发一个中文', 'age'=>22));
http://www.bkjia.com/phpjc/1049130.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1049130.htmltecharticlephp 利用socket发送http请求(get,post),socketget 今天给大家带来的是如何利用socket发送get,post请求。我借用燕十八老师封装好的一个http类给...