~会飞的鱼

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1 <?php
  2 /****
  3 自学it网 高端PHP培训
  4 
  5 论  坛: http://www.zixue.it
  6 微  博: http://weibo.com/Yshiba
  7 ****/
  8 
  9 
 10 
 11 /*
 12 PHP+socket编程 发送HTTP请求
 13 
 14 要求能 模拟下载,注册,登陆,批量发帖
 15 */
 16 
 17 
 18 // http请求类的接口
 19 interface Proto {
 20     // 连接url
 21     function conn($url);
 22 
 23     //发送get查询
 24     function get();
 25 
 26     // 发送post查询
 27     function post();
 28 
 29     // 关闭连接
 30     function close();
 31 }
 32 
 33 
 34 
 35 class Http implements Proto {
 36 
 37     const CRLF  = "\r\n";
 38 
 39     protected $errno = -1;
 40     protected $errstr = '';
 41     protected $response = '';
 42 
 43     protected $url = null;
 44     protected $version = 'HTTP/1.1';
 45     protected $fh = null;
 46     
 47     protected $line = array();
 48     protected $header = array();
 49     protected $body = array();
 50 
 51     
 52     public function __construct($url) {
 53         $this->conn($url);
 54         $this->setHeader('Host: ' . $this->url['host']);
 55     }
 56 
 57     // 此方法负责写请求行
 58     protected function setLine($method) {
 59         $this->line[0] = $method . ' ' . $this->url['path'] . ' '. $this->version;
 60     }
 61 
 62     // 此方法负责写头信息
 63     protected function setHeader($headerline) {
 64         $this->header[] = $headerline; 
 65     }
 66 
 67     // 此方法负责写主体信息
 68     protected function setBody($body) {
 69          $this->body[] = http_build_query($body);
 70     }
 71 
 72 
 73     // 连接url
 74     public function conn($url) {
 75         $this->url = parse_url($url);
 76         // 判断端口
 77         if(!isset($this->url['port'])) {
 78             $this->url['port'] = 80;
 79         }
 80 
 81         $this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3);
 82     }
 83 
 84     //构造get请求的数据
 85     public function get() {
 86         $this->setLine('GET');
 87         $this->request();
 88         return $this->response;
 89     }
 90 
 91     // 构造post查询的数据
 92     public function post($body = array()) {      
 93         $this->setLine('POST');
 94 
 95         // 设计content-type
 96         $this->setHeader('Content-type: application/x-www-form-urlencoded');
 97         
 98         // 设计主体信息,比GET不一样的地方
 99         $this->setBody($body);
100 
101 
102         // 计算content-length
103         $this->setHeader('Content-length: ' . strlen($this->body[0]));
104 
105         $this->request();
106     }
107 
108     // 真正请求
109     public function request() {
110         // 把请求行,头信息,实体信息 放在一个数组里,便于拼接
111         $req = array_merge($this->line,$this->header,array(''),$this->body,array(''));
112         //print_r($req);
113 
114         $req = implode(self::CRLF,$req); 
115         //echo $req; exit;
116 
117         fwrite($this->fh,$req);
118         
119         while(!feof($this->fh)) {
120             $this->response .= fread($this->fh,1024);
121         }
122 
123         $this->close(); // 关闭连接
124     }
125 
126 
127     // 关闭连接
128     public function close() {
129         fclose($this->fh);
130     }
131 
132     
133 
134 }
135 
136 
137 
138 
139 /*
140 $url = 'http://news.163.com/13/0613/09/9187CJ4C00014JB6.html';
141 
142 $http = new Http($url);
143 echo $http->get();
144 */
145 /*
146 set_time_limit(0);
147 
148 $url = 'http://liangyue.net.cn/0523/?';
149 
150 
151 
152 for($i=1;$i<100;$i++) {
153     $str = str_shuffle('abcdefghijklmnopqrst0776656');
154     $tit = substr($str,0,5);
155     $con = substr($str,6,8);
156 
157     $http = new Http($url);
158     $http->post(array('tit'=>$tit,'con'=>$con,'submit'=>'留言'));
159 
160     echo $tit,'-----------',$con,'<br />';
161 
162     usleep(2000);
163 }
164 
165 */

 

posted on 2013-09-02 17:42  ~会飞的鱼  阅读(553)  评论(0编辑  收藏  举报