PHP HTTP request class, support Get,post,multipart/form-data
HttpRequest.class.php
<?php/** HttpRequest class, HTTP request classes, support get,post,multipart/form-data* date:2013-09-25* author:fdipzone* Ver: 1.0** func:* Public setconfig Setting Connection parameters * Public setformdata setting form data * Public setfiledata setting file data * Public Send data * Private Connect Create connection * Private Disconnect disconnect * Private Sendget get method, processing the data sent, not at Data * Private Sendpost post mode, processing the data sent * private Sendmultipart multipart way, processing the sent data, sending files recommended Use this way */class httpreques t{//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 no IP is 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 the 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 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 way, processing the data sent, does not process the file data Private function Sendget () {//check if empty data if (! $this->_formdata) { return false; }//processing 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, processing the Sent data private function sendpost () {//check if empty 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 ($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.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 way to process the sent data, send file is recommended to use this method private function Sendmultipart () {//check if empty data if (! $this->_form Data && $this->_filedata) {return false; }//Set the split identifier 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 "; Arrays use JSON encode to facilitate processing of}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-dispo Sition: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) {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?>
demo
<?phprequire (' HttpRequest.class.php '); $config = array (' IP ' = = ' dem O.fdipzone.com ',//if empty use host 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); $ob J->setfiledata ($filedata), $result = $obj->send (' get '),//$result = $obj->send (' post ');//$result = $obj Send (' multipart '); Echo ' <pre> ';p rint_r ($result); Echo ' </pre> ';?
The above is the PHP HTTP request class, support get,post,multipart/form-data content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!
Related articles:
Php method for getting header information for HTTP requests
PHP Get header Information implementation steps for HTTP request
PHP implementation of HTTP request Encapsulation example