PHP supports HTTP request classes of GET, POST, Multipart/form-data, and multipartform-data.

Source: Internet
Author: User

PHP supports HTTP request classes of GET, POST, Multipart/form-data, and multipartform-data.

The example in this article describes the HTTP request class for PHP to support GET, POST, Multipart/form-data and its applications. The details are as follows:

The HttpRequest. class. php class file is as follows:

<? Php/** HttpRequest class, HTTP request class, support for GET, POST, Multipart/form-data * Date: * Author: fdipzone * Ver: 1.0 ** Func: * public setConfig set connection parameter * public setFormdata set form data * public setFiledata set file data * public send data * private connect create connection * private disconnect * private sendGet method, processes sent data, does not process file data * private sendPost post, processes sent data * private sendMultipart multipart, processes sent data, and sends files This method is recommended */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 the connection parameter 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 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?>

The demo sample program is as follows:

<? Php require ('httprequest. class. php '); $ config = array ('IP' => '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 '<pre> '; print_r ($ result); echo '</pre>';?>

You can click here to download the complete instance code.

I hope this article will help you with PHP programming.


[URGENT] I'm not sure whether the attribute of enctype = "multipart/form-data" is a version issue or not.

A file upload control that can only be obtained in the background using $ _ FILES. Other control types are obtained using $ _ POST, you only need to use the value obtained by $ _ FILES for your processing.

Js simulate POST submission of an enctype = "multipart/form-data" type form

It is used only when a file is uploaded.
XmlHttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ;");
Change
XmlHttp. setRequestHeader ("Content-Type", "multipart/form-data ;");

As for sending binary data, you can solve it by yourself.
----------------------------- 7db8c30150364 is actually regular
It is an end segment of the Start segment. 7db8c30150364 only uses a string of non-repeated characters to identify the data in the middle, Content-Disposition: form-data; name = "polls []" indicates the data and file name.

In fact, it is necessary to use this method when uploading files in a socket package. If you have time to study how to handle POST files in HTTP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.