-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcurlGetPage.php
128 lines (104 loc) · 4.08 KB
/
curlGetPage.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
124
125
126
127
<?php
// Version: 01 May 2016
// Added support of cookie file and post query
//
function httpGET ( $url, $useCache = true, $cookie = null, $referer = null )
{
$cacheFileName = '';
// Check if we have cache
// - Extract domain part from URL
if (preg_match("#^http(s){0,1}\:\/\/(.+?)(\/.+)$#", $url, $m)) {
$xDomainName = $m[2];
$xURL = str_replace("/", ":", $m[3]);
if ($useCache && is_dir("loader") && (is_dir("loader/".$xDomainName) || mkdir("loader/".$xDomainName))) {
$cacheFileName = "loader/".$xDomainName."/".$xURL.".html";
} else {
$useCache = false;
}
// Check if we can load cached data
if ($useCache && is_file($cacheFileName)) {
$fw = fopen($cacheFileName, "r");
$content = fread($fw, filesize($cacheFileName));
fclose($fw);
$header = array(
'http_code' => '200',
'content' => $content,
);
return $header;
}
}
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => false, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt',
);
if (($cookie != null) && (strlen($cookie) > 1)) {
$options[CURLOPT_COOKIE] = $cookie;
}
if (($referer != null) && (strlen($referer) > 1)) {
$options[CURLOPT_REFERER] = $referer;
}
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
if ($useCache && ($header['http_code'] == "200")) {
$fw = fopen($cacheFileName, "w");
fwrite($fw, $content);
fclose($fw);
}
return $header;
}
//
function httpPOST($url, $postKeys, $cookie=null, $referer=null) {
$curlKeys = join("&", array_map(function($x){ return $x[0]."=".urlencode($x[1]); }, $postKeys));
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $curlKeys,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt',
);
if (($cookie != null) && (strlen($cookie) > 1)) {
$options[CURLOPT_COOKIE] = $cookie;
}
if (($referer != null) && (strlen($referer) > 1)) {
$options[CURLOPT_REFERER] = $referer;
}
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
$header['isComplete'] = ($header['http_code'] == "200")?true:false;
return $header;
}