PHP Custom Class Fsocket The method of simulating post or GET request _php tips

Source: Internet
Author: User
Tags explode php class strlen urlencode

This example describes the PHP custom class Fsocket method for simulating post or get requests. Share to everyone for your reference. Specifically as follows:

zsocket.class.php files are 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); }
}

The demo code is as follows:

$host = ' www.jb51.net ';
$port = ' no ';
$type = ' POST ';
$file = '/';
$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";
Print_r ($header);
Print_r ($data);
echo "-->\r\n";
* *
var_dump ($header);
Var_dump ($data);

I hope this article will help you with your PHP program design.

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.