-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp.php
More file actions
73 lines (67 loc) · 2.21 KB
/
Http.php
File metadata and controls
73 lines (67 loc) · 2.21 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
<?php
/**
* Created by 不辞远.
* User: Administrator
* Date: 2015/4/11
* Time: 16:42
*/
class Helper_Http
{
public static function get($url, $header = array(),&$setcookie = "")
{
$ch = curl_init();
$needheader = empty($setcookie) ? 0 : 1;
$opt = array(
CURLOPT_URL => $url,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 0,
CURLOPT_HEADER=>$needheader
);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt_array($ch, $opt);
$result = curl_exec($ch);
if ($needheader == 1) {
list($header, $body) = explode("\r\n\r\n", $result, 2);
preg_match_all('/Set-Cookie:(.*);/iU', $header, $str);
if (!empty($str[1])) {
$setcookie = implode('; ', $str[1]) . ";";
}
return $body;
}
return $result;
}
public static function post($url, $data, $header = array(),&$setcookie = "")
{
$ch = curl_init();
$needheader = empty($setcookie) ? 0 : 1;
$opt = array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data,
CURLOPT_URL => $url,
CURLOPT_FOLLOWLOCATION=>0,
CURLOPT_HEADER=>$needheader
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt_array($ch, $opt);
$result = curl_exec($ch);
if ($needheader == 1) {
list($header, $body) = explode("\r\n\r\n", $result, 2);
preg_match_all('/Set-Cookie: (.*;)/iU', $header, $str);
if (!empty($str[1])) {
$setcookie = implode('; ', $str[1]) . ";";
}
return $body;
}
return $result;
}
}