This article mainly introduces php user-defined fsocket method for simulating post or get requests. It involves some skills related to php's use of socket to simulate post and get requests. It has some reference value, for more information, see
This article mainly introduces php user-defined fsocket method for simulating post or get requests. It involves some skills related to php's use of socket to simulate post and get requests. It has some reference value, for more information, see
This article describes how to simulate post or get requests using fsocket, a custom php class. Share it with you for your reference. The details are as follows:
The zsocket. class. php file is as follows:
<? Phpclass ZSocket {/** Init */private function _ fsockopen ($ host, $ port, & $ errno, & $ errstr, $ timeout) {$ ip = @ gethostbyname ($ host); $ s = @ socket_create (AF_INET, SOCK_STREAM, 0); if (socket_set_nonblock ($ s )) {$ r = @ socket_connect ($ s, $ ip, $ port); if ($ r | socket_last_error () = EINPROGRESS) {$ errno = EINPROGRESS; return $ s ;}$ errno = socket_last_error ($ s); $ errstr = socket_strerror ($ errno); socket_c Lose ($ s); return false;}/** set Cookie */private function _ setCookie ($ cookie) {$ _ cookies = explode (";", $ cookie ); $ _ tmp = explode ("=", $ _ cookies [0]); setcookie ($ _ tmp [0], $ _ tmp [1]); return $ _ cookies;}/** get the returned data header content */private function _ getDataHeader (& $ fp, & $ reHeader, & $ cookies) {$ maxlen = 0; while (! Feof ($ fp) {line line = fgets ($ fp, 1024); if (substr ($ line, 0, 12) = 'set-Cookie :') {$ cookies [] = $ this-> _ setCookie (substr ($ line, 12);} $ reHeader. = $ line; if (substr ($ line, 0, 16) = 'content-Length: ') {$ maxlen = intval (substr ($ line, 16, -2);} if ($ line = "\ r \ n" | $ line = "") break;} return $ maxlen ;} /** get the body content of the returned data */private function _ getDataBody (& $ fp, $ maxlen) {$ reData = ""; while (! Feof ($ fp) {$ line = fgets ($ fp, $ maxlen + 1); $ reData. = $ line; if (strlen ($ line) <$ maxlen) $ maxlen = $ maxlen-strlen ($ line); else break;} return $ reData ;} /** set and return the content of the header to be sent */public function get_HeaderInfo ($ host, $ type = 'get', $ file =' http://www.jb51.net/ ', $ Params = array (), $ head = array (), $ cookies = array () {$ _ params = $ _ cookies = ''; if (is_array ($ params) {foreach ($ params as $ key => $ value) {$ _ params. = "&". $ key. "= ". urlencode ($ value) ;}$ _ params = (strlen ($ _ params)> 1 )? Substr ($ _ params, 1): '';} else if (is_string ($ params) {$ _ params = urlencode ($ params );} foreach ($ cookies as $ key => $ value) {$ _ cookies. = ";". $ key. "= ". urlencode ($ value) ;}$ _ cookies = (strlen ($ _ cookies)> 2 )? Substr ($ _ cookies, 2): ''; $ file. = ($ type = 'get ')? ($ _ Params = ''? '':'? '. $ _ Params): ''; $ header = $ type. "". $ file. "HTTP/1.1 \ r \ n"; $ header. = "Host :". $ host. "\ r \ n"; // $ header. = "Referer :". get_ip (). "\ r \ n"; // $ header. = "X-Forwarded-:". get_ip (). "\ r \ n"; $ header. = ($ type = 'get ')? '':" Content-Type: application/x-www-form-urlencoded \ r \ n "; if (is_array ($ head) & $ head! = Array () {foreach ($ head as $ k => $ v) {$ header. = $ k. ":". $ v. "\ r \ n" ;}}$ header. = "Content-Length :". strlen ($ _ params ). "\ r \ n"; if ($ _ cookies! = '') $ Header. = "Cookie :". $ _ cookies. "\ r \ n";/* foreach ($ _ SERVER as $ name => $ value) {if (substr ($ name, 0, 5) = 'HTTP _ '& $ name! = 'HTTP _ host') {$ header. = str_replace ('', '-', ucwords (strtolower (str_replace ('_','', substr ($ name, 5 ))))). ":". $ value. "\ r \ n" ;}} */$ header. = "Connection: Close \ r \ n"; $ header. = $ _ params. "\ r \ n"; return $ header;}/** send and return the result Array */public function get_SendData ($ host, $ port = 80, $ header = '') {if (function_exists ('fsockopen') {$ fp = fsockopen ($ host, $ port, $ errno, $ errstr, 10 );} else if (function_e Xists ('pfsockopen') {$ fp = pfsockopen ($ host, $ port, $ errno, $ errstr, 10 );} else if (function_exists ('stream _ socket_client ') {$ fp = stream_socket_client ($ host. ':'. $ port, $ errno, $ errstr, 10);} else {$ fp = $ this-> _ fsockopen ($ host, $ port, $ errno, $ errstr, 10) ;}$ fp = fsockopen ($ host, $ port, $ errno, $ errstr, 10); if (! $ Fp) return array ('header' => '', 'data' => $ errstr. "---> ". $ errno, 'cookies' => ''); $ reHeader = $ reData =" "; $ cookies = array (); fputs ($ fp, $ header ); $ maxlen = $ this-> _ getDataHeader ($ fp, $ reHeader, $ cookies); $ reData = $ this-> _ getDataBody ($ fp, $ maxlen ); fclose ($ fp); return array ('header' => $ reHeader, 'data' => $ reData, 'cookies' => $ cookies );}}
The demo code is as follows:
$ Host = 'www .jb51.net '; $ port = '80'; $ type = 'post'; $ file = 'HTTP: // www.jb51.net/'{}params = ''; // include_once ('include/zsocket. class. php '); // include $ zsk = new ZSocket (); $ header = $ zsk-> get_HeaderInfo ($ host, $ type, $ file, $ params ); $ data = $ zsk-> get_SendData ($ host, $ port, $ header);/* echo" \ R \ n "; */var_dump ($ header); var_dump ($ data );
I hope this article will help you with php programming.
,