HttpRequest.class.php
<?php/** HttpRequest class, HTTP request class, support Get,post,multipart/form-data * date:2013-09-25 * author:fdipzone * ver:1.0 * * Func: * Public setconfig Setting Connection parameters * Public setformdata setting up form data * Public setfiledata Set File data * Public Send data * Private Connect Create connection * Private Disconnect disconnect * Private send Get-get-way, processing sent data, does not process file data * Private Sendpost post way, processing sent data * Private Sendmultipart multipart way, processing sent
data, send file recommended Use this way/class httprequest{//class start private $_ip = ';
Private $_host = ';
Private $_url = ';
Private $_port = ';
Private $_errno = ';
Private $_errstr = ';
Private $_timeout = 15;
Private $_FP = null;
Private $_formdata = Array ();
Private $_filedata = Array ();
Set connection parameters Public function Setconfig ($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;
If IP is not set, use host instead of if ($this->_ip== ') {$this->_ip = $this->_host; }//Set form data public function Setformdata ($formdata =array ()) {$this->_formdat
A = $formdata; //Set File data Public function Setfiledata ($filedata =array ()) {$this->_filedata = $fil
Edata;
//Send data Public function send ($type = ' get ') {$type = Strtolower ($type); Check Send type
if (!in_array ($type, Array (' Get ', ' post ', ' multipart '))} {return false; //Check connection if ($this->connect ()) {switch ($type) {case '
Get ': $out = $this->sendget ();
Break
Case ' post ': $out = $this->sendpost ();
Break
Case ' multipart ': $out = $this->sendmultipart ();
Break
}//NULL data if (! $out) {return false;
}//Send data fputs ($this->_fp, $out);
Read the return data $response = ';
while ($row = Fread ($this->_fp, 4096)) {$response. = $row;
//Disconnect $this->disconnect (); $pos = Strpos ($response, "rnrn");
$response = substr ($response, $pos +4);
return $response;
}else{return false; }//Create a connection//view this column more highlights: Http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/privat e function Connect () {$this->_fp = fsockopen ($this->_ip, $this->_port, $this->_errno, $this->_err
STR, $this->_timeout);
if (! $this->_fp) {return false;
return true; //Disconnect Private Function disconnect () {if ($this->_fp!=null) {fclose
($this->_FP);
$this->_FP = null;
}//Get mode, processing sent data, does not process file data Private function Sendget () {//Check for empty data
if (! $this->_formdata) {return false; }//Process URL $url = $this->_url. '? '.
Http_build_query ($this->_formdata);
$out = "Get" $url. "Http/1.1rn"; $out. = "Host:". $this->_host. "
RN ";
$out. = "Connection:closernrn";
return $out; //Post mode, handle sent data private function sendpost () {//check if NULL data if (! $thi
S->_formdata &&! $this->_filedata) {return false; }//form data $data = $this->_formdata?
$this->_formdata:array ();
File data if ($this->_filedata) {foreach ($this->_filedata as $filedata) { if (file_exists ($filedata [' path ']) {$data [$filedata [' name ']] = file_get_contents ($filedata [' Path '])
;
}} if (! $data) {return false;
} $data = Http_build_query ($data); $out = "POST" $this->_url. "Http/1.1rn"; $out. = "Host:". $this->_host. "
RN ";
$out. = "CONTENT-TYPE:APPLICATION/X-WWW-FORM-URLENCODEDRN"; $out. = "Content-length:". strlen ($data).
RN ";
$out. = "Connection:closernrn";
$out. = $data;
return $out; ///multipart mode, processing the sent data, send the file recommended to use this method private function Sendmultipart () {//check whether
Null data if (! $this->_formdata &&! $this->_filedata) {return false;
//Set the split identification Srand (double) microtime () *1000000);
$boundary = '---------------------------'. substr (MD5 (rand (0,32000)), 0,10); $data = '. $boundary.
RN ";
form data $formdata = '; foreach ($this->_formdata as $key => $val) {$formdata. = "Content-disposition:form-data; Name= "". $key. ""
RN "; $formdata. = "Content-type:Text/plainrnrn "; if (Is_array ($val)) {$formdata. = Json_encode ($val). " RN "; The array uses JSON encode to facilitate processing of}else{$formdata. = Rawurlencode ($val).
RN "; $formdata. = '-$boundary. '
RN ";
}//File data $filedata = '; foreach ($this->_filedata as $val) {if (File_exists ($val [' path ')]) {$filedata. = "Content -disposition:form-data; Name= "". $val [' name ']. ""; Filename= "". $val [' filename ']. ""
RN "; $filedata. = "Content-type:". Mime_content_type ($val [' Path ']). "
Rnrn "; $filedata. = Implode (', File ($val [' Path ']). "
RN "; $filedata. = '-$boundary.
RN ";
} if (! $formdata &&! $filedata) {return false; $data. = $formdata. $filedata. "
--rnrn "; $out = "POST". $this->_url. " Http/1.1rn "; $out. = "Host:". $this->_host. "
RN "; $out. = "Content-type:multipart/form-data; Boundary= ". $boundary."
RN "; $out. = "Content-length:". strlen ($data).
RN ";
$out. = "Connection:closernrn";
$out. = $data;
return $out; }//Class end?>