PHP语言 HTTP请求类,支持GET,POST,Multipart、form-data
小标 2018-07-12 来源 : 阅读 920 评论 0

摘要:本文主要向大家介绍了PHP语言 HTTP请求类,支持GET,POST,Multipart、form-data,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

本文主要向大家介绍了PHP语言 HTTP请求类,支持GET,POST,Multipart、form-data,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

php HTTP请求类,支持GET,POST,Multipart、form-dataPHP HTTP请求类,支持GET,POST,Multipart/form-data

HttpRequest.class.php

[php] view plain copy

1. <?php  

2. /** HttpRequest class, HTTP请求类,支持GET,POST,Multipart/form-data 

3. *   Date:   2013-09-25 

4. *   Author: fdipzone 

5. *   Ver:    1.0 

6. * 

7. *   Func: 

8. *   public  setConfig     设置连接参数 

9. *   public  setFormdata   设置表单数据 

10. *   public  setFiledata   设置文件数据 

11. *   public  send          发送数据 

12. *   private connect       创建连接 

13. *   private disconnect    断开连接 

14. *   private sendGet       get 方式,处理发送的数据,不会处理文件数据 

15. *   private sendPost      post 方式,处理发送的数据 

16. *   private sendMultipart multipart 方式,处理发送的数据,发送文件推荐使用此方式 

17. */  

18.   

19. class HttpRequest{ // class start  

20.   

21.     private $_ip = '';  

22.     private $_host = '';  

23.     private $_url = '';  

24.     private $_port = '';  

25.     private $_errno = '';  

26.     private $_errstr = '';  

27.     private $_timeout = 15;  

28.     private $_fp = null;  

29.       

30.     private $_formdata = array();  

31.     private $_filedata = array();  

32.   

33.   

34.     // 设置连接参数  

35.     public function setConfig($config){  

36.         $this->_ip = isset($config['ip'])? $config['ip'] : '';  

37.         $this->_host = isset($config['host'])? $config['host'] : '';  

38.         $this->_url = isset($config['url'])? $config['url'] : '';  

39.         $this->_port = isset($config['port'])? $config['port'] : '';  

40.         $this->_errno = isset($config['errno'])? $config['errno'] : '';  

41.         $this->_errstr = isset($config['errstr'])? $config['errstr'] : '';  

42.         $this->_timeout = isset($confg['timeout'])? $confg['timeout'] : 15;  

43.   

44.         // 如没有设置ip,则用host代替  

45.         if($this->_ip==''){  

46.             $this->_ip = $this->_host;  

47.         }  

48.     }  

49.   

50.   

51.     // 设置表单数据  

52.     public function setFormData($formdata=array()){  

53.         $this->_formdata = $formdata;  

54.     }  

55.   

56.   

57.     // 设置文件数据  

58.     public function setFileData($filedata=array()){  

59.         $this->_filedata = $filedata;  

60.     }  

61.   

62.   

63.     // 发送数据  

64.     public function send($type='get'){  

65.   

66.         $type = strtolower($type);  

67.   

68.         // 检查发送类型  

69.         if(!in_array($type, array('get','post','multipart'))){  

70.             return false;  

71.         }  

72.   

73.         // 检查连接  

74.         if($this->connect()){  

75.   

76.             switch($type){  

77.                 case 'get':  

78.                     $out = $this->sendGet();  

79.                     break;  

80.   

81.                 case 'post':  

82.                     $out = $this->sendPost();  

83.                     break;  

84.   

85.                 case 'multipart':  

86.                     $out = $this->sendMultipart();  

87.                     break;  

88.             }  

89.   

90.             // 空数据  

91.             if(!$out){  

92.                 return false;  

93.             }  

94.   

95.             // 发送数据  

96.             fputs($this->_fp, $out);  

97.   

98.             // 读取返回数据  

99.             $response = '';  

100.   

101.             while($row = fread($this->_fp, 4096)){  

102.                 $response .= $row;  

103.             }  

104.   

105.             // 断开连接  

106.             $this->disconnect();  

107.   

108.             $pos = strpos($response, "\r\n\r\n");  

109.             $response = substr($response, $pos+4);  

110.   

111.             return $response;  

112.   

113.         }else{  

114.             return false;  

115.         }  

116.     }  

117.   

118.   

119.     // 创建连接  

120.     private function connect(){  

121.         $this->_fp = fsockopen($this->_ip, $this->_port, $this->_errno, $this->_errstr, $this->_timeout);  

122.         if(!$this->_fp){  

123.             return false;  

124.         }  

125.         return true;  

126.     }  

127.   

128.   

129.     // 断开连接  

130.     private function disconnect(){  

131.         if($this->_fp!=null){  

132.             fclose($this->_fp);  

133.             $this->_fp = null;  

134.         }  

135.     }  

136.   

137.   

138.     // get 方式,处理发送的数据,不会处理文件数据  

139.     private function sendGet(){  

140.   

141.         // 检查是否空数据  

142.         if(!$this->_formdata){  

143.             return false;  

144.         }  

145.   

146.         // 处理url  

147.         $url = $this->_url.'?'.http_build_query($this->_formdata);  

148.           

149.         $out = "GET ".$url." http/1.1\r\n";  

150.         $out .= "host: ".$this->_host."\r\n";  

151.         $out .= "connection: close\r\n\r\n";  

152.   

153.         return $out;  

154.     }  

155.   

156.   

157.     // post 方式,处理发送的数据  

158.     private function sendPost(){  

159.   

160.         // 检查是否空数据  

161.         if(!$this->_formdata && !$this->_filedata){  

162.             return false;  

163.         }  

164.   

165.         // form data  

166.         $data = $this->_formdata? $this->_formdata : array();  

167.   

168.         // file data  

169.         if($this->_filedata){  

170.             foreach($this->_filedata as $filedata){  

171.                 if(file_exists($filedata['path'])){  

172.                     $data[$filedata['name']] = file_get_contents($filedata['path']);  

173.                 }  

174.             }  

175.         }  

176.   

177.         if(!$data){  

178.             return false;  

179.         }  

180.   

181.         $data = http_build_query($data);  

182.   

183.         $out = "POST ".$this->_url." http/1.1\r\n";  

184.         $out .= "host: ".$this->_host."\r\n";  

185.         $out .= "content-type: application/x-www-form-urlencoded\r\n";  

186.         $out .= "content-length: ".strlen($data)."\r\n";  

187.         $out .= "connection: close\r\n\r\n";  

188.         $out .= $data;  

189.   

190.         return $out;  

191.     }  

192.   

193.   

194.     // multipart 方式,处理发送的数据,发送文件推荐使用此方式  

195.     private function sendMultipart(){  

196.   

197.         // 检查是否空数据  

198.         if(!$this->_formdata && !$this->_filedata){  

199.             return false;  

200.         }  

201.   

202.         // 设置分割标识  

203.         srand((double)microtime()*1000000);  

204.         $boundary = '---------------------------'.substr(md5(rand(0,32000)),0,10);  

205.   

206.         $data = '--'.$boundary."\r\n";  

207.   

208.         // form data  

209.         $formdata = '';  

210.   

211.         foreach($this->_formdata as $key=>$val){  

212.             $formdata .= "content-disposition: form-data; name=\"".$key."\"\r\n";  

213.             $formdata .= "content-type: text/plain\r\n\r\n";  

214.             if(is_array($val)){  

215.                 $formdata .= json_encode($val)."\r\n"; // 数组使用json encode后方便处理  

216.             }else{  

217.                 $formdata .= rawurlencode($val)."\r\n";  

218.             }  

219.             $formdata .= '--'.$boundary."\r\n";  

220.         }  

221.   

222.         // file data  

223.         $filedata = '';  

224.   

225.         foreach($this->_filedata as $val){  

226.             if(file_exists($val['path'])){  

227.                 $filedata .= "content-disposition: form-data; name=\"".$val['name']."\"; filename=\"".$val['filename']."\"\r\n";  

228.                 $filedata .= "content-type: ".mime_content_type($val['path'])."\r\n\r\n";  

229.                 $filedata .= implode('', file($val['path']))."\r\n";  

230.                 $filedata .= '--'.$boundary."\r\n";  

231.             }  

232.         }  

233.   

234.         if(!$formdata && !$filedata){  

235.             return false;  

236.         }  

237.   

238.         $data .= $formdata.$filedata."--\r\n\r\n";  

239.   

240.         $out = "POST ".$this->_url." http/1.1\r\n";  

241.         $out .= "host: ".$this->_host."\r\n";  

242.         $out .= "content-type: multipart/form-data; boundary=".$boundary."\r\n";  

243.         $out .= "content-length: ".strlen($data)."\r\n";  

244.         $out .= "connection: close\r\n\r\n";  

245.         $out .= $data;  

246.   

247.         return $out;  

248.     }  

249.   

250. } // class end  

251.   

252. ?>  

demo

[php] view plain copy

1. <?php  

2.   

3. require('HttpRequest.class.php');  

4.   

5. $config = array(  

6.             'ip' => 'demo.fdipzone.com', // 如空则用host代替  

7.             'host' => 'demo.fdipzone.com',  

8.             'port' => 80,  

9.             'errno' => '',  

10.             'errstr' => '',  

11.             'timeout' => 30,  

12.             'url' => '/getapi.php',  

13.             //'url' => '/postapi.php',  

14.             //'url' => '/multipart.php'  

15. );  

16.   

17. $formdata = array(  

18.     'name' => 'fdipzone',  

19.     'gender' => 'man'  

20. );  

21.   

22. $filedata = array(  

23.     array(  

24.         'name' => 'photo',  

25.         'filename' => 'photo.jpg',  

26.         'path' => 'photo.jpg'  

27.     )  

28. );  

29.   

30. $obj = new HttpRequest();  

31. $obj->setConfig($config);  

32. $obj->setFormData($formdata);  

33. $obj->setFileData($filedata);  

34. $result = $obj->send('get');  

35. //$result = $obj->send('post');  

36. //$result = $obj->send('multipart');  

37.   

38. echo '<pre>';  

39. print_r($result);  

40. echo '</pre>';  

41.   

42. ?>  


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言PHP频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程