服务器之家

服务器之家 > 正文

php自定义类fsocket模拟post或get请求的方法

时间:2020-11-13 12:02     来源/作者:ZongLiang

本文实例讲述了php自定义类fsocket模拟postget请求的方法。分享给大家供大家参考。具体如下:

zsocket.class.php文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?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;
 }
 /*
  * 设置Cookie
 */
 private function _setCookie($cookie){
  $_cookies = explode("; ",$cookie);
  $_tmp = explode("=",$_cookies[0]);
  setcookie($_tmp[0], $_tmp[1]);
  return $_cookies;
 }
 /*
  * 获取返回数据header内容
 */
 private function _getDataHeader(&$fp,&$reHeader,&$cookies){
  $maxlen = 0;
  while(!feof($fp)){
   $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;
 }
 /*
  * 获取返回数据正文内容
 */
 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;
 }
 /*
  * 设置并返回要发送的header内容
 */
 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;
 }
 /*
  * 发送,并返回结果 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);
 }
}

demo代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$host = 'www.zzvips.com';
$port = '80';
$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);

希望本文所述对大家的php程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部