HTTP协议分析系列(五)------php+socket编程发送http请求

来源:互联网 发布:东莞房价知乎 编辑:程序博客网 时间:2024/06/06 15:05

一、PHP+socket请求原理

二、模拟POST请求

 

三、封装自己的HTTP类

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

四、测试功能,实现批量发帖功能

(一)、建立数据表

 

(二)、程序实现

  (1)、建立liuyanben.php

  

[php] view plain copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5.   
  6. </head>  
  7. <body>  
  8. <?php  
  9. $link=mysql_connect('localhost','root','root');  
  10. mysql_select_db('test');  
  11. mysql_set_charset('utf8');  
  12.   
  13. if(!empty($_POST)){  
  14.     $sql="insert liuyanben(id,title,content) values(null,'{$_POST['title']}','{$_POST['content']}')";  
  15.     mysql_query($sql);  
  16. }  
  17.   
  18. $sql="select * from liuyanben";  
  19.   
  20. $result=mysql_query($sql);  
  21. while($row=mysql_fetch_assoc($result)){  
  22.     $list[]=$row;  
  23. }  
  24. ?>  
  25. <form method='post' action='liuyanben.php'>  
  26. <table border='1'>  
  27.     <tr>  
  28.         <td>编号</td>  
  29.         <td>标题</td>  
  30.         <td>内容</td>  
  31.     </tr>  
  32.     <?php  
  33.     foreach($list as $key=>$value){  
  34.     ?>  
  35.     <tr>  
  36.         <td><?php echo $value['id'];?></td>  
  37.         <td><?php echo $value['title'];?></td>  
  38.         <td><?php echo $value['content'];?></td>  
  39.     </tr>  
  40.     <?php   
  41.     }  
  42.     ?>  
  43.       
  44. </table>  
  45.     <input type='text' name='title'><br/>  
  46.     <input type='text' name='content'><br/>  
  47.     <input type='submit' value='提交'>  
  48. </form>  
  49. </body>  
  50. </html>  

(2).实现批量发帖

 

[php] view plain copy
  1. set_time_limit(0);  
  2. $url='http://localhost/socket/liuyanben.php';  
  3.   
  4. for($i=1;$i<100;$i++){  
  5.     $str=str_shuffle('abcdefghigklmnopqrstuvwxyz');  
  6.     $title=substr($str,0,5);  
  7.     $content=substr($str,6,8);  
  8.     $http=new Http($url);  
  9.     $http->post(array('title'=>$title,'content'=>$content,'submit'=>'留言'));  
  10.     echo $title.'--------'.$con.'<br/>';  
  11.     usleep(2000);  
  12. }  
(三)、实验结果

 

查看页面

0 0
原创粉丝点击