Socket操作类

/**
002. * Socket请求类
003. *
004. * @author  aiden
005. * @copyright zmh
006. * @example 
007. *
008. *  //GET请求
009. *  $http = new HttpClient();
010. *  $http->setHeader('cookie','name=name;');  //传递cookie
011. *  $http->setHeader('Referer','http://www.myzmh.com');  //设置来源网址
012. *
013. *  $result = $http->get('http://localhost/get.php');
014. *  if (!$result)
015. *  {
016. *         echo $http->getError();
017. *  }
018. *
019. *  //POST请求
020. *  $http = new HttpClient();
021. *  $result = $http->post('http://localhost/post.php',array('my'=>'name'));
022. *  if (!$result)
023. *  {
024. *         echo $http->getError();
025. *  }
026.
027.
028. */
029.  
030. class HttpClient
031. {
032. private $error '';  //错误信息
033.  
034. private $post array();  //POST参数
035.  
036. private $header array();  //头部信息
037.  
038. private $method 'GET';  //SOCKET请求类型
039.  
040. private $url '';  //请求网址
041.  
042. private $body '';  //返回内容
043.  
044. private $args array();  //socket需要的参数
045.  
046. private $timeout = 30;  //超时时间
047.  
048. private $returnHeader array();  //返回头部信息
049.  
050. private $data array();
051.  
052. public function setTimeout($time)
053. {
054. $time intval($time);
055. if ($time > 0)
056. {
057. $this->timeout = $time;
058. }
059. }
060.  
061. /**
062. * Socket GET请求
063. *
064. * @param unknown_type $url
065. */
066. public function get($url,$data=array())
067. {
068. $this->method = 'GET';
069. if (!emptyempty($data))
070. {
071. $query $this->getQueryString($data);
072. if (strpos($url,'?') !== false)
073. {
074. $url .= '&'.$query;
075. }
076. else
077. {
078. $url .= '?'.$query;
079. }
080. }
081.  
082. return $this->doSocket($url);
083. }
084.  
085.  
086. /**
087. * Socket POST请求
088. *
089. * @param string $url 请求网址
090. * @param string or array 请求数据
091. */  
092. public function post($url,$data=array())
093. {
094. $this->method = 'POST';
095. if (!emptyempty($data))
096. {
097. $this->data = $this->getQueryData($data);
098. }
099.  
100. return $this->doSocket($url);
101. }          
102.  
103. /**
104. * 进行Sookie请求,返回内容
105. *
106. * @param string $url
107. */
108. private function doSocket($url)
109. {
110. $url_array = @parse_url($url);
111. $port = isset($url_array['port']) ? $url_array['port'] : 80;
112. $this->args['path'] = isset($url_array['path']) ? $url_array['path'] : '/';
113. $this->args['host'] = $url_array['host'];
114. $header $this->doHeader();
115. $fp '';
116. if (!$fp = @fsockopen($this->args['host'],$port,$errorno,$errstr,$this->timeout))
117. {
118. $this->addError('创建Szyocket错误,请检测网址是否正确! ');
119. return false;
120.
121.  
122. if (!fwrite($fp,$header))
123. {
124. fclose($fp);
125. $this->addError('写入头部信息错误');
126. return false;           
127. }
128.  
129. $data '';
130. while (!feof($fp))
131. {  
132. stream_set_timeout($fp$this->timeout);
133. $data .= fgets($fp, 1024); 
134. $info = stream_get_meta_data($fp);           
135. if ($info['timed_out']) {
136. $this->addError('获取数据超时');
137. fclose($fp);
138. return false;
139. }     
140. }
141.  
142. $pos strpos($data"\r\n\r\n");
143. $head substr($data, 0, $pos);    //http head
144. $array explode("\r\n",$head);
145.  
146. if (is_array($array))
147. {
148. foreach ($array as $key=>$val)
149. {
150. if ($key > 0)
151. {
152. list($_head,$_info) = explode(':',$val);
153. $this->returnHeader[$_head] = trim($_info);
154. }
155. else
156. {
157. list(,$status,) = explode(' ',$val);
158. }
159.  
160. }
161. }
162.  
163. if ($status != 200)
164. {
165. $this->addError('请求网址错误,状态码:'.$status);
166. return false;
167. }
168.  
169. $this->body = substr($data$pos + 4, strlen($data) - ($pos + 4));//主体部份  
170.  
171. return  $this->body;           
172. }
173.  
174. /**
175. * 生成头部请求信息
176. *
177. * @return string
178. */
179. private function doHeader()
180. {
181. $header $this->method.' '.$this->args['path']." HTTP/1.1\r\n";
182. $header .= 'Host: '.$this->args['host']."\r\n";
183. if (!isset($this->header['Accept']))
184. {
185. $this->header['Accept'] = '*/*';
186. }
187.  
188. if (!isset($this->header['User-Agent']))
189. {
190. $this->header['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)';
191. }
192.  
193. if (!isset($this->header['Accept-Language']))
194. {
195. $this->header['Accept-Language'] = 'zh-cn';
196. }
197.  
198. if ($this->method == 'POST')
199. {
200. $this->header['Content-type'] = 'application/x-www-form-urlencoded';
201. $this->header['Content-length'] = strlen($this->data);
202. }          
203.  
204. foreach ($this->header as $key=>$val)
205. {
206. $header .= $key.': '.$val."\r\n";
207. }
208.  
209. $header .= "Connection: Close\r\n\r\n";
210. if ($this->data)
211. {
212. $header .= $this->data."\r\n";
213. }
214. $this->data = '';
215. $this->header = array();
216. return $header;            
217. }
218.  
219. /**
220. * 将一个数组组合成GET参数,如:  array('name'=>'zmh','id'=>1) 转换为 : name=zmh&id=1
221. *
222. * @param array $data
223. */
224. private function getQueryData($data)
225. {
226. $querystring ''
227. if (is_array($data)) { 
228. foreach ($data as $key => $val)
229.
230. if (is_array($val))
231.
232. foreach ($val as $val2)
233.
234. $querystring .= urlencode($key).'='.urlencode($val2).'&'
235.
236. }
237. else
238.
239. $querystring .= urlencode($key).'='.urlencode($val).'&'
240.
241.
242. $querystring substr($querystring, 0, -1); // Eliminate unnecessary & 
243. else 
244. $querystring $data
245.
246. return $querystring;      
247. }
248.  
249. /**
250. * 返回头部信息
251. *
252. * @return array
253. */
254. public function getHeader()
255. {
256. return $this->returnHeader; 
257. }
258.  
259. /**
260. * 设置头部信息
261. *
262. * @param  string $name 名称
263. * @param string $value 值
264. * @example
265. *    $coll->setHeader('Accept-Language','zh-cn');
266. */
267. public function setHeader($name,$value)
268. {
269. $this->header[ucfirst($name)] = $value
270. }
271.  
272. /**
273. * 设置错误信息
274. *
275. * @param string $msg
276. */
277. private function addError($msg)
278. {
279. $this->error = $msg;
280. }
281.  
282. /**
283. * 返回错误信息
284. *
285. * @param string $msg
286. */
287. public function getError()
288. {
289. return $this->error;
290. }
291.  
292.  
293. /**
294. * 得到返回的内容
295. *
296. * @return string
297. */
298. public function getBody()
299. {
300. return $this->body;
301. }
302. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值