当前位置:首页 > PHP教程 > php应用 > 列表

php 利用socket发送GET,POST请求的实例代码

发布:smiling 来源: PHP粉丝网  添加日期:2022-03-16 15:10:33 浏览: 评论:0 

作为php程序员一定会接触http协议,也只有深入了解http协议,编程水平才会更进一步,最近我一直在学习php的关于http的编程,许多东西恍然大悟,受益匪浅,希望分享给大家,本文需要有一定http基础的开发者阅读。

今天给大家带来的是如何利用socket发送GET,POST请求,我借用燕十八老师封装好的一个Http类给进行说明。

在日常编程中相信很多人和我一样大部分时间是利用浏览器向服务器提出GET,POST请求,那么可否利用其它方式提出GET,POST请求呢?答案必然是肯定的,了解过HTTP协议的人知道,浏览器提交请求的实质是向服务器发送一个请求信息,这个请求信息有请求行,请求头,请求体(非必须)构成。服务器根据请求信息返回一个响应信息,连接断开。

HTTP请求的格式如下所示:

  1. <request-line> 
  2. <headers> 
  3. <blank line> 
  4. [<request-body>] 

HTTP响应的格式与请求的格式十分相似:

  1. <status-line> 
  2. <headers> 
  3. <blank line> 
  4.  
  5. [<response-body>] 

我们可以利用HTTP发送请求的原理,可以重新考虑利用socket发送HTTP请求。

Socket的英文原义是“孔”或“插座”,通常也称作“套接字”,用于描述IP地址和端口,是一个通信链的句柄,可以用来实现不同虚拟机或不同计算机之间的通信。在Internet上的主机一般运行了多个服务软件,同时提供几种服务,每种服务都打开一个Socket,并绑定到一个端口上,不同的端口对应于不同的服务,如此看来,其实利用socket操作远程文件和读写本地的文件一样容易,把本地文件看成通过硬件传输,远程文件通过网线传输就行了。

因而可以将发送请求的考虑成 建立连接->打开socket接口(fsockopen())->写入请求(fwrite())->读出响应(fread()->关闭文件(fclose())。话不多说,直接上代码:

  1. <?php  
  2.  
  3. interface Proto { 
  4.   // 连接url 
  5.   function conn($url); 
  6.  
  7.   //发送get查询 
  8.   function get(); 
  9.  
  10.   // 发送post查询 
  11.   function post(); 
  12.  
  13.   // 关闭连接 
  14.   function close(); 
  15. } 
  16.  
  17.  
  18.  
  19. class Http implements Proto { 
  20.  
  21.   const CRLF = "\r\n"; 
  22.  
  23.   protected $errno = -1; 
  24.   protected $errstr = ''; 
  25.   protected $response = ''; 
  26.  
  27.   protected $url = null; 
  28.   protected $version = 'HTTP/1.1'; 
  29.   protected $fh = null; 
  30.     
  31.   protected $line = array(); 
  32.   protected $header = array(); 
  33.   protected $body = array(); 
  34.  
  35.     
  36.   public function __construct($url) { 
  37.     $this->conn($url); 
  38.     $this->setHeader('Host: ' . $this->url['host']); 
  39.   } 
  40.  
  41.   // 此方法负责写请求行 
  42.   protected function setLine($method) { 
  43.     $this->line[0] = $method . ' ' . $this->url['path'] . '?' .$this->url['query'] . ' '. $this->version; 
  44.   } 
  45.  
  46.   // 此方法负责写头信息 
  47.   public function setHeader($headerline) { 
  48.     $this->header[] = $headerline;  
  49.   } 
  50.  
  51.   // 此方法负责写主体信息 
  52.   protected function setBody($body) { 
  53.      $this->body[] = http_build_query($body); 
  54.   } 
  55.  
  56.  
  57.   // 连接url 
  58.   public function conn($url) { 
  59.     $this->url = parse_url($url); 
  60.     // 判断端口 
  61.     if(!isset($this->url['port'])) { 
  62.       $this->url['port'] = 80; 
  63.     } 
  64.  
  65.     // 判断query 
  66.     if(!isset($this->url['query'])) { 
  67.       $this->url['query'] = ''; 
  68.     } 
  69.  
  70.     $this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3); 
  71.   } 
  72.  
  73.   //构造get请求的数据 
  74.   public function get() { 
  75.     $this->setLine('GET'); 
  76.     $this->request(); 
  77.     return $this->response; 
  78.   } 
  79.  
  80.   // 构造post查询的数据 
  81.   public function post($body = array()) {    
  82.     $this->setLine('POST'); 
  83.  
  84.     // 设计content-type 
  85.     $this->setHeader('Content-type: application/x-www-form-urlencoded'); 
  86.       
  87.     // 设计主体信息,比GET不一样的地方 
  88.     $this->setBody($body); 
  89.  
  90.  
  91.     // 计算content-length 
  92.     $this->setHeader('Content-length: ' . strlen($this->body[0])); 
  93.  
  94.     $this->request(); 
  95.  
  96.     return $this->response; 
  97.   } 
  98.  
  99.   // 真正请求 
  100.   public function request() { 
  101.     // 把请求行,头信息,实体信息 放在一个数组里,便于拼接 
  102.     $req = array_merge($this->line,$this->header,array(''),$this->body,array('')); 
  103.     //print_r($req); 
  104.  
  105.     $req = implode(self::CRLF,$req);  
  106.     //echo $req; exit; 
  107.  
  108.     fwrite($this->fh,$req); 
  109.       
  110.     while(!feof($this->fh)) { 
  111.       $this->response .= fread($this->fh,1024); 
  112.     } 
  113.  
  114.     $this->close(); // 关闭连接 
  115.   } 
  116.  
  117.  
  118.   // 关闭连接 
  119.   public function close() { 
  120.     fclose($this->fh); 
  121.   } 
  122.  
  123. } 

利用此类发送一个简单的GET请求:

  1. <?php//记得引用Http类 
  2. $url="http://home.cnblogs.com/u/DeanChopper/"; 
  3. $http=new Http($url); 
  4.  
  5. $response=$http->get(); 
  6.  
  7. print_r($response); 

返回值为信息,可以对响应信息进行进一步处理,得到自己想得到的内容。

Tags: socket GET POST

分享到: