This article describes the PHP implementation of Get,post,multipart/form-data HTTP request class and its application, share for everyone to reference. Specifically as follows:
The HttpRequest.class.php class files are as follows:
<?php/** HttpRequest class, HTTP request class, support Get,post,multipart/form-data * date:2013-09-25 * author:fdipzone * Ve r:1.0 * * Func: * Public setconfig Setting Connection parameters * Public setformdata setting form data * Public setfiledata setting file data * Publi C Send data * Private Connect Create connection * Private Disconnect disconnect * Private sendget get way, processing sent data, not processing file data * Private Sendpost post mode, processing the sent data * Private Sendmultipart multipart way, processing the sent data, send the file recommended Use this method * * 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->_formdata = $formdata;
//Set File data Public function Setfiledata ($filedata =array ()) {$this->_filedata = $filedata;
//Send data Public function send ($type = ' get ') {$type = Strtolower ($type);
Check the Send type if (!in_array ($type, Array (' Get ', ' post ', ' multipart '))} {return false; //Check connection if ($this->connect ()) {switch ($type) {case ' get ': $out = $this->se
Ndget ();
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, "\r\n\r\n");
$response = substr ($response, $pos +4);
return $response;
}else{return false; }//Create connection Private function connect () {$this->_fp = fsockopen ($this->_ip, $this->_port, $this-&G
T;_errno, $this->_errstr, $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 whether NULL data if (! $this->_formdata
) {return false; //Process 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 mode, processing the Sent data private function sendpost () {//check whether NULL data if (! $this->_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) ($file
data[' path ']) {$data [$filedata [' name ']] = file_get_contents ($filedata [' path ']);
}} if (! $data) {return false;
} $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 mode, processing the sent data, send the file recommended this way private function Sendmultipart () {//check whether NULL data if (! $this->_form
Data &&! $this->_filedata) {return false;
//Set the split identification Srand (double) microtime () *1000000);
$boundary = '---------------------------'. substr (MD5 (rand (0,32000)), 0,10); $data = '. $boundary.
\ r \ n ";
form data $formdata = '; foreach ($this->_formdata as $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 ";
Array uses JSON encode to facilitate processing}else{ $formdata. = Rawurlencode ($val).
\ r \ n "; $formdata. = '-$boundary. '
\ r \ n ";
}//File data $filedata = '; foreach ($this->_filedata as $val) {if (File_exists ($val [' path ')]) {$filedata. = "Content-disposition:fo Rm-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) {return false; $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;
}}//Class end?>
The Demo sample program is as follows:
<?php
require (' HttpRequest.class.php ');
$config = Array (
' IP ' => ' demo.fdipzone.com ',//null) instead
of ' host ' => ' demo.fdipzone.com ', '
port ' = >,
' errno ' => ', '
errstr ' => ', ' timeout ' => ', '
url ' => '/getapi.php ',
//' URL ' => '/postapi.php ',
//' URL ' => '/multipart.php '
);
$formdata = Array (
' name ' => ' Fdipzone ', '
gender ' => ' man '
);
$filedata = Array (
' name ' => ' photo ', '
filename ' => ' photo.jpg ',
' path ' => ') Photo.jpg '
)
);
$obj = new HttpRequest ();
$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> ';
? >
Full instance code can click here to download this site.
I hope this article will help you with your PHP program design.