PHP supports HTTP request classes such as GET, POST, Multipart, and form-data.

Source: Internet
Author: User
This article mainly introduces php http request classes that support GET, POST, and Multipartform-data, including connection and processing methods and related skills, for more information about how to implement HTTP request classes and applications that support GET, POST, Multipart, and form-data in PHP, see the following example. 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'
'; print_r($result); echo '
';?>

You can click here to download the complete instance code.

I hope this article will help you with PHP programming.

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.