-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetpage.php
More file actions
executable file
·160 lines (136 loc) · 6.01 KB
/
getpage.php
File metadata and controls
executable file
·160 lines (136 loc) · 6.01 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
# 使用CURL获取网页内容,报头,状态码,mime类型和编码 charset
# CURLOPT_CONNECTTIMEOUT 请求连接超时
# CURLOPT_TIMEOUT 响应数据传输时允许时间
date_default_timezone_set("America/Los_Angeles");# 洛杉矶时间
$t = date('ymdH',time());
$log_dir = './log/';
if(!is_dir($log_dir)) mkdir($log_dir,0777);
$log = $log_dir . 'vps-' . date('ymd',time()) . '.log';
if (file_exists($log)) {
echo file_get_contents($log);
exit;
}
$cookie_dir = './cookie/';
if(!is_dir($cookie_dir)) mkdir($cookie_dir,0777);
$cookie_file = $cookie_dir . time() . '.cookie';
setcookie("PHPSESSID", "vc0heoa6lfsi3gger54pkns152");
$url = 'https://secure.hostsolutions.ro';
$dologin = $url . '/dologin.php';
$clientarea = $url . '/clientarea.php';
$details = $clientarea . '?action=productdetails&id=5246&rrd=0&timeframe=hour&language=chinese';
$token = getResponse($clientarea, [], $cookie_file);
preg_match('/<input type="hidden" name="token" value="(.*)"/U', $token, $match);
// print_r($match);
$post['token'] = $match[1];
$post['password'] = 'password';
$url = 'http://ysuo.org';
$res_array = getResponse($url);
echo '<pre>';
# print_r($res_array);
echo $res_array['body'];
// unlink($cookie_file);
# 支持GET和POST,返回值网页内容,报头,状态码,mime类型和编码 charset
function getResponse($url, $data = [], $cookie_file = ''){
$url_array = parse_url($url);
$host = $url_array['scheme'] . '://' . $url_array['host'];
if(!empty($_SERVER['HTTP_REFERER'])) $refer = $_SERVER['HTTP_REFERER'];
else $refer = $host . '/';
if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
else $lang = 'zh-CN,zh;q=0.9';
if(!empty($_SERVER['HTTP_USER_AGENT'])) $agent = $_SERVER['HTTP_USER_AGENT'];
else $agent = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36';
// $agent = 'Wget/1.18 (mingw32)'; # 'Wget/1.17.1 (linux-gnu)';
// echo "<pre>\r\n" . $agent . "\r\n" . $refer . "\r\n" . $lang . "\r\n\r\n";
if(empty($cookie_file)){
$cookie_file = '.cookie';
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $refer);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-Language: " . $lang));
if(!empty($data)){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); # 302 重定向
curl_setopt($ch, CURLOPT_AUTOREFERER, true); # 301 重定向
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); # 取cookie的参数是
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); # 发送cookie
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
# try{}catch{}语句
// try{
// $handles = curl_exec($ch);
// curl_close($ch);
// return $handles;
// }
// catch(Exception $e){
// echo 'Caught exception:', $e -> getMessage(), "\n";
// }
// unlink($cookie_file);
$res_array = explode("\r\n\r\n", $result, 2);
$headers = explode("\r\n", $res_array[0]);
$status = explode(' ', $headers[0]);
# 如果$headers为空,则连接超时
if(empty($res_array[0])) die('<br><br><center><b>连接超时</b></center>');
# 如果$headers状态码为404,则自定义输出页面。
if($status[1] == '404') die("<pre><b>找不到,The requested URL was not found on this server.</b>\r\n\r\n$res_array[0]</pre>\r\n\r\n");
# 如果$headers第一行没有200,则连接异常。
# if($status[1] !== '200') die("<pre><b>连接异常,状态码: $status[1]</b>\r\n\r\n$res_array[0]</pre>\r\n\r\n");\
if($status[1] !== '200'){
$body_array = explode("\r\n\r\n", $res_array[1], 2);
$header_all = $res_array[0] . "\r\n\r\n" . $body_array[0];
$res_array[0] = $body_array[0];
$body = $body_array[1];
}else{
$header_all = $res_array[0];
$body = $res_array[1];
}
$headers = explode("\r\n", $res_array[0]);
$status = explode(' ', $headers[0]);
$headers[0] = str_replace('HTTP/1.1', 'HTTP/1.1:', $headers[0]);
foreach($headers as $header){
if(stripos(strtolower($header), 'content-type:') !== FALSE){
$headerParts = explode(' ', $header);
$mime_type = trim(strtolower($headerParts[1]));
//if(!empty($headerParts[2])){
// $charset_array = explode('=', $headerParts[2]);
// $charset = trim(strtolower($charset_array[1]));
//}
}
if(stripos(strtolower($header), 'charset') !== FALSE){
$charset_array = explode('charset=', $header);
$charset = trim(strtolower($charset_array[1]));
}else{
$charset = preg_match("/<meta.+?charset=[^\w]?([-\w]+)/i", $res_array[1], $temp) ? strtolower($temp[1]):"";
}
}
if(empty($charset)) $charset = 'utf-8';
if(strstr($charset, ';')){
$charset_array = '';
$charset_array = explode(';', $charset);
$charset = trim($charset_array[0]);
//$charset = str_replace(';', '', $charset);
}
if(strstr($mime_type, 'text/html') and $charset !== 'utf-8'){
$body = mb_convert_encoding ($body, 'utf-8', $charset);
}
# $body = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $body);
# echo "<pre>\r\n$header_all\r\n\r\n" . "$status[1]\r\n$mime_type\r\n$charset\r\n\r\n";
# header($res_array[0]);
$res_array = array();
$res_array['header'] = $header_all;
$res_array['status'] = $status[1];
$res_array['mime_type'] = $mime_type;
$res_array['charset'] = $charset;
$res_array['body'] = $body;
return $res_array;
}