This example describes the PHP custom class Fsocket method for simulating post or get requests. Share to everyone for your reference. Specifically as follows:
<?php class 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_close ($s);
return false;
* * * Set cookies/Private function _setcookie ($cookie) {$_cookies = explode (";", $cookie);
$_tmp = explode ("=", $_cookies[0]);
Setcookie ($_tmp[0], $_tmp[1]);
return $_cookies;
* * Get Return header content/Private Function _getdataheader (& $fp,& $reHeader,& $cookies) {$maxlen = 0;
while (!feof ($fp)) {$line = Fgets ($fp, 1024);
if (substr ($line, 0,) = = ' Set-cookie: ') {$cookies [] = $this->_setcookie (substr ($line, 12));}
$reHeader. = $line; if (substr ($line, 0,) = = ' CoNtent-length: ') {$maxlen = Intval (substr ($line, 16,-2));
if ($line = = "\ r \ n" | | $line = = "") break;
return $maxlen;
* * Get Return Data body content/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 header content to send/Public function get_headerinfo ($host, $type = ' get ', $file = '/', $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-for:". 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\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_exists (' 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, ' cookie ' => ');
$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, ' cookie ' => $cookies); }
}
I hope this article will help you with your PHP program design.