In php, the easiest way to process data asynchronously is to use fsockopen. next I will introduce the asynchronous processing based on the fsockopen function. I hope it will help you. example test. the php code is as follows :? In phpnb... php, the simplest way to process data asynchronously is to use fsockopen. next I will introduce the asynchronous processing based on fsockopen function. I hope it will help you.
Example: test. phpThe code is as follows:
The code for test4.php is as follows:
Add an asynchronous processing class that can request HTTP and HTTPS protocols, and handle 301, 302 redirection, and GZIP compression. the code is as follows.
The code for asynHandle. class. php is as follows:
Url = '') {return false;} $ url_array = parse_url ($ this-> url );! Isset ($ url_array ['host']) & $ url_array ['host'] = '';! Isset ($ url_array ['path']) & $ url_array ['path'] = '';! Isset ($ url_array ['query']) & $ url_array ['query'] = '';! Isset ($ url_array ['port']) & $ url_array ['port'] = 80; $ this-> host = $ url_array ['host']; $ this-> port = $ url_array ['port']; $ this-> referer = $ url_array ['scheme ']. '://'. $ this-> host. '/'; $ this-> requestUri = $ url_array ['path']? $ Url_array ['path']. ($ url_array ['query']? '? '. $ Url_array ['query']: ''): '/'; switch ($ url_array ['scheme ']) {case 'https ': $ this-> fop = fsockopen ('SSL ://'. $ this-> host, 443, $ errno, $ errstr, $ this-> timeout); break; default: $ this-> fop = fsockopen ($ this-> host, $ this-> port, $ errno, $ errstr, $ this-> timeout); break;} if (! $ This-> fop) {$ this-> result = "$ errstr ($ errno)
\ N "; return false;} return true;} // analyzeUrl end // assemble the HTTP header private function assHeader () {$ method = emptyempty ($ this-> post )? 'Get': 'post'; $ gzip = $ this-> gzip? 'Gzip, ': ''; // cookie data if (! Emptyempty ($ htis-> cookie) {$ htis-> cookie = http_build_cookie ($ htis-> cookie);} // post data if (! Emptyempty ($ this-> post) {$ this-> post = http_build_query ($ this-> post );} $ header = "$ method $ this-> requestUri HTTP/1.0 \ r \ n"; $ header. = "Accept: */* \ r \ n"; $ header. = "Referer: $ this-> referer \ r \ n"; $ header. = "Accept-Language: zh-cn \ r \ n"; if (! Emptyempty ($ this-> post) {$ header. = "Content-Type: application/x-www-form-urlencoded \ r \ n";} $ header. = "User-Agent: $ _ SERVER [HTTP_USER_AGENT] \ r \ n"; $ header. = "Host: $ this-> host \ r \ n"; if (! Emptyempty ($ this-> post) {$ header. = 'content-Length :'. strlen ($ this-> post ). "\ r \ n" ;}$ header. = "Connection: Close \ r \ n"; $ header. = "Accept-Encoding: {$ gzip} deflate \ r \ n"; $ header. = "Cookie: $ this-> cookie \ r \ n"; $ header. = $ this-> post; $ this-> header = $ header;} // assHeader end // return status Detection. private function checkRecvHeader ($ header) is redirected to 301 and 302) {if (strstr ($ header, '20140901') | strstr ($ header, '20140901 ')) {// Redirect processing preg_match ("/Location :(.*?) $/Im ", $ header, $ match); $ url = trim ($ match [1]); preg_match ("/Set-Cookie :(.*?) $/Im ", $ header, $ match); $ cookie = (emptyempty ($ match ))? '': $ Match [1]; $ obj = new AsynHandle (); $ result = $ obj-> Get ($ url, $ cookie, $ this-> post ); $ this-> result = $ result; return $ result;} elseif (! Strstr ($ header, '000000') {// The domain name or URL return false cannot be found;} else return 200 ;} // checkRecvHeader end // gzip unzip private function gzdecode ($ data) {$ flags = ord (substr ($ data, 3, 1); $ headerlen = 10; $ extralen = 0; $ filenamelen = 0; if ($ flags & 4) {$ extralen = unpack ('v', substr ($ data, 10, 2 )); $ extralen = $ extralen [1]; $ headerlen + = 2 + $ extralen;} if ($ flags & 8) $ headerlen = strpos ($ data, chr (0 ), $ headerl En) + 1; if ($ flags & 16) $ headerlen = strpos ($ data, chr (0), $ headerlen) + 1; // open source software: phpfensi.com if ($ flags & 2) $ headerlen + = 2; $ unpacked = @ gzinflate (substr ($ data, $ headerlen); if ($ unpacked = FALSE) $ unpacked = $ data; return $ unpacked;} // gzdecode end // The Request function. only requests are sent. public function Request ($ url, $ cookie = array () is not returned (), $ post = array (), $ timeout = 3) {$ this-> url = $ url; $ this-> cookie = $ cookie; $ this-> Post = $ post; $ this-> timeout = $ timeout; if (! $ This-> analyzeUrl () {return $ this-> result;} $ this-> assHeader (); stream_set_blocking ($ this-> fop, 0 ); // non-blocking, no need to wait for fwrite ($ this-> fop, $ this-> header); fclose ($ this-> fop); return true ;} // Request end // Get the function, Request and return the public function Get ($ url, $ cookie = array (), $ post = array (), $ timeout = 30) {$ this-> url = $ url; $ this-> cookie = $ cookie; $ this-> post = $ post; $ this-> timeout = $ timeout; if (! $ This-> analyzeUrl () {return $ this-> result;} $ this-> assHeader (); stream_set_blocking ($ this-> fop, $ this-> block ); stream_set_timeout ($ this-> fop, $ this-> timeout); fwrite ($ this-> fop, $ this-> header ); $ status = stream_get_meta_data ($ this-> fop); if (! $ Status ['timed _ out']) {$ h = ''; while (! Feof ($ this-> fop) {if ($ header = @ fgets ($ this-> fop )) & ($ header = "\ r \ n" | $ header = "\ n") {break;} $ h. = $ header;} $ checkHttp = $ this-> checkRecvHeader ($ h); if ($ checkHttp! = 200) {return $ checkHttp;} $ stop = false; $ return = ''; $ this-> gzip = false; if (strstr ($ h, 'gzip ')) $ this-> gzip = true; while (! ($ Stop & $ status ['timed _ out'] & feof ($ this-> fop) {if ($ status ['timed _ out']) return false; $ data = fread ($ this-> fop, ($ this-> limit = 0 | $ this-> limit> 128? 128: $ this-> limit); if ($ data = '') {// Some servers do not work, you must determine FOEF break;} $ return. = $ data; if ($ this-> limit) {$ this-> limit-= strlen ($ data); $ stop = $ this-> limit <= 0 ;}} @ fclose ($ this-> fop); $ this-> result = $ this-> gzip? $ This-> gzdecode ($ return): $ return; return $ this-> result;} else {return false ;}// Get end }?>
Permanent link:
Reprint at will! Include the article address.