PHP实现支持GET,POST,Multipart/form-data的HTTP请求类
本文实例讲述了PHP实现支持GET,POST,Multipart/form-data的HTTP请求类及其应用,分享给大家供大家参考。具体如下:
HttpRequest.class.php类文件如下:
<?php /**HttpRequestclass,HTTP请求类,支持GET,POST,Multipart/form-data *Date:2013-09-25 *Author:fdipzone *Ver:1.0 * *Func: *publicsetConfig设置连接参数 *publicsetFormdata设置表单数据 *publicsetFiledata设置文件数据 *publicsend发送数据 *privateconnect创建连接 *privatedisconnect断开连接 *privatesendGetget方式,处理发送的数据,不会处理文件数据 *privatesendPostpost方式,处理发送的数据 *privatesendMultipartmultipart方式,处理发送的数据,发送文件推荐使用此方式 */ classHttpRequest{//classstart private$_ip=''; private$_host=''; private$_url=''; private$_port=''; private$_errno=''; private$_errstr=''; private$_timeout=15; private$_fp=null; private$_formdata=array(); private$_filedata=array(); //设置连接参数 publicfunctionsetConfig($config){ $this->_ip=isset($config['ip'])?$config['ip']:''; $this->_host=isset($config['host'])?$config['host']:''; $this->_url=isset($config['url'])?$config['url']:''; $this->_port=isset($config['port'])?$config['port']:''; $this->_errno=isset($config['errno'])?$config['errno']:''; $this->_errstr=isset($config['errstr'])?$config['errstr']:''; $this->_timeout=isset($confg['timeout'])?$confg['timeout']:15; //如没有设置ip,则用host代替 if($this->_ip==''){ $this->_ip=$this->_host; } } //设置表单数据 publicfunctionsetFormData($formdata=array()){ $this->_formdata=$formdata; } //设置文件数据 publicfunctionsetFileData($filedata=array()){ $this->_filedata=$filedata; } //发送数据 publicfunctionsend($type='get'){ $type=strtolower($type); //检查发送类型 if(!in_array($type,array('get','post','multipart'))){ returnfalse; } //检查连接 if($this->connect()){ switch($type){ case'get': $out=$this->sendGet(); break; case'post': $out=$this->sendPost(); break; case'multipart': $out=$this->sendMultipart(); break; } //空数据 if(!$out){ returnfalse; } //发送数据 fputs($this->_fp,$out); //读取返回数据 $response=''; while($row=fread($this->_fp,4096)){ $response.=$row; } //断开连接 $this->disconnect(); $pos=strpos($response,"\r\n\r\n"); $response=substr($response,$pos+4); return$response; }else{ returnfalse; } } //创建连接 privatefunctionconnect(){ $this->_fp=fsockopen($this->_ip,$this->_port,$this->_errno,$this->_errstr,$this->_timeout); if(!$this->_fp){ returnfalse; } returntrue; } //断开连接 privatefunctiondisconnect(){ if($this->_fp!=null){ fclose($this->_fp); $this->_fp=null; } } //get方式,处理发送的数据,不会处理文件数据 privatefunctionsendGet(){ //检查是否空数据 if(!$this->_formdata){ returnfalse; } //处理url $url=$this->_url.'?'.http_build_query($this->_formdata); $out="GET".$url."http/1.1\r\n"; $out.="host:".$this->_host."\r\n"; $out.="connection:close\r\n\r\n"; return$out; } //post方式,处理发送的数据 privatefunctionsendPost(){ //检查是否空数据 if(!$this->_formdata&&!$this->_filedata){ returnfalse; } //formdata $data=$this->_formdata?$this->_formdata:array(); //filedata if($this->_filedata){ foreach($this->_filedataas$filedata){ if(file_exists($filedata['path'])){ $data[$filedata['name']]=file_get_contents($filedata['path']); } } } if(!$data){ returnfalse; } $data=http_build_query($data); $out="POST".$this->_url."http/1.1\r\n"; $out.="host:".$this->_host."\r\n"; $out.="content-type:application/x-www-form-urlencoded\r\n"; $out.="content-length:".strlen($data)."\r\n"; $out.="connection:close\r\n\r\n"; $out.=$data; return$out; } //multipart方式,处理发送的数据,发送文件推荐使用此方式 privatefunctionsendMultipart(){ //检查是否空数据 if(!$this->_formdata&&!$this->_filedata){ returnfalse; } //设置分割标识 srand((double)microtime()*1000000); $boundary='---------------------------'.substr(md5(rand(0,32000)),0,10); $data='--'.$boundary."\r\n"; //formdata $formdata=''; foreach($this->_formdataas$key=>$val){ $formdata.="content-disposition:form-data;name=\"".$key."\"\r\n"; $formdata.="content-type:text/plain\r\n\r\n"; if(is_array($val)){ $formdata.=json_encode($val)."\r\n";//数组使用jsonencode后方便处理 }else{ $formdata.=rawurlencode($val)."\r\n"; } $formdata.='--'.$boundary."\r\n"; } //filedata $filedata=''; foreach($this->_filedataas$val){ if(file_exists($val['path'])){ $filedata.="content-disposition:form-data;name=\"".$val['name']."\";filename=\"".$val['filename']."\"\r\n"; $filedata.="content-type:".mime_content_type($val['path'])."\r\n\r\n"; $filedata.=implode('',file($val['path']))."\r\n"; $filedata.='--'.$boundary."\r\n"; } } if(!$formdata&&!$filedata){ returnfalse; } $data.=$formdata.$filedata."--\r\n\r\n"; $out="POST".$this->_url."http/1.1\r\n"; $out.="host:".$this->_host."\r\n"; $out.="content-type:multipart/form-data;boundary=".$boundary."\r\n"; $out.="content-length:".strlen($data)."\r\n"; $out.="connection:close\r\n\r\n"; $out.=$data; return$out; } }//classend ?>
demo示例程序如下:
<?php require('HttpRequest.class.php'); $config=array( 'ip'=>'demo.fdipzone.com',//如空则用host代替 'host'=>'demo.fdipzone.com', 'port'=>80, 'errno'=>'', 'errstr'=>'', 'timeout'=>30, 'url'=>'/getapi.php', //'url'=>'/postapi.php', //'url'=>'/multipart.php' ); $formdata=array( 'name'=>'fdipzone', 'gender'=>'man' ); $filedata=array( array( 'name'=>'photo', 'filename'=>'photo.jpg', 'path'=>'photo.jpg' ) ); $obj=newHttpRequest(); $obj->setConfig($config); $obj->setFormData($formdata); $obj->setFileData($filedata); $result=$obj->send('get'); //$result=$obj->send('post'); //$result=$obj->send('multipart'); echo'<pre>'; print_r($result); echo'</pre>'; ?>
完整实例代码可以点击此处本站下载。
希望本文所述对大家的PHP程序设计有所帮助。