PHPHTTP request class, which supports GET, POST, and Multipartform-data
Php http request class, supporting GET, POST, Multipart/form-data
HttpRequest. class. php
_ 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 address is set, use host instead of if ($ this-> _ ip = '') {$ this-> _ ip = $ this-> _ host ;}// set the form data public function setFormData ($ formdata = array ()) {$ this-> _ formdata = $ formdata;} // sets the public function setFileData ($ filedata = array () {$ this-> _ filedata = $ filedata ;} // send data public function send ($ type = 'get') {$ type = strtolower ($ type); // Check the sending 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;} // empty data if (! $ Out) {return false;} // send data fputs ($ this-> _ fp, $ out); // read the returned data $ response = ''; while ($ row = fread ($ this-> _ fp, 4096) {$ response. = $ row;} // disconnect $ this-> disconnect (); $ pos = strpos ($ response, "\ r \ n "); $ response = substr ($ response, $ pos + 4); return $ response;} else {return false ;}// create a connection to private function connect () {$ this-> _ fp = fsockopen ($ this-> _ ip, $ this-> _ port, $ this-> _ errno, $ this-> _ er Rstr, $ this-> _ timeout); if (! $ This-> _ fp) {return false;} return true;} // disconnect the private function disconnect () {if ($ this-> _ fp! = Null) {fclose ($ this-> _ fp); $ this-> _ fp = null ;}// get method to process sent data, file data will not be processed private function sendGet () {// check whether empty 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"; return $ out;} // post method, processing sent data private function sendPost () {// check whether 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"; $ out. = $ data; return $ out;} // multipart method to process sent data. this method is recommended for sending files. private function sendMultipart () {// check whether null data is returned if (! $ This-> _ formdata &&! $ This-> _ filedata) {return false;} // sets the split ID srand (double) microtime () * 1000000); $ boundary = '---------------------------'. substr (md5 (rand (),); $ 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"; if (is_array ($ val) {$ form Data. = json_encode ($ val ). "\ r \ n"; // The array is easy to process after json encode is used} 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: form-data; name = \"". $ val ['name']. "\"; filename = \"". $ val ['filename']. "\" \ r \ n "; $ filedata. = "content-type :". mime_content_type ($ Val ['path']). "\ r \ n"; $ filedata. = implode ('', file ($ val ['path']). "\ r \ n"; $ filedata. = '--'. $ boundary. "\ r \ n" ;}} if (! $ Formdata &&! $ Filedata) {return false;} $ data. = $ formdata. $ filedata. "-- \ 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"; $ out. = $ data; return $ out; }}// class end?>
Demo
'Demo .fdipzone.com ', // if it is null, use host instead of '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 ('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'';print_r($result);echo '
';?>
The above is the php HTTP request class, which supports GET, POST, Multipart/form-data. For more information, see PHP Chinese website (www.php1.cn )!
Related articles:
Php methods for retrieving http request header information
PHP to obtain the http request header information
Example of http request encapsulation implemented by php