1
|
|
|
<?php
|
2
|
|
|
/*
|
3
|
|
|
*
|
4
|
|
|
*/
|
5
|
|
|
namespace KS\HTTP;
|
6
|
|
|
|
7
|
|
|
class HTTP {
|
8
|
|
|
|
9
|
|
|
private $cookiePath = null;
|
10
|
|
|
private $userAgent = 'Googlebot/2.1 (+http://www.google.com/bot.html)';
|
11
|
|
|
private $timeout = 60;
|
12
|
|
|
private $headers = array();
|
13
|
|
|
|
14
|
|
|
public function __construct($cookiePath = null) {
|
15
|
|
|
if(!empty($cookiePath)) {
|
16
|
|
|
$this->cookiePath = $cookiePath;
|
17
|
|
|
}
|
18
|
|
|
}
|
19
|
|
|
|
20
|
|
|
public function setUserAgent($userAgent) {
|
21
|
|
|
$this->userAgent = $userAgent;
|
22
|
|
|
}
|
23
|
|
|
|
24
|
|
|
public function setCookiePath($path) {
|
25
|
|
|
$this->cookiePath = $path;
|
26
|
|
|
}
|
27
|
|
|
|
28
|
|
|
public function setTimeout($timeout) {
|
29
|
|
|
$this->timeout = $timeout;
|
30
|
|
|
}
|
31
|
|
|
|
32
|
|
|
/*
|
33
|
|
|
* Method Get
|
34
|
|
|
*/
|
35
|
3 |
|
public function get($url, $referer = null){
|
36
|
3 |
|
$ch = curl_init();
|
37
|
3 |
|
curl_setopt($ch, CURLOPT_URL, $url);
|
38
|
3 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
39
|
3 |
|
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
|
40
|
3 |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
41
|
3 |
|
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
|
42
|
3 |
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
43
|
|
|
|
44
|
3 |
|
if (!empty($referer)) {
|
45
|
|
|
curl_setopt($ch, CURLOPT_REFERER, $referer);
|
46
|
|
|
}
|
47
|
|
|
|
48
|
3 |
|
if(!empty($this->cookiePath)){
|
49
|
|
|
curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiePath);
|
50
|
|
|
curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiePath);
|
51
|
|
|
}
|
52
|
|
|
|
53
|
3 |
|
if(!empty($this->headers)) {
|
54
|
|
|
curl_setopt ($ch, CURLOPT_HTTPHEADER, $this->headers);
|
55
|
|
|
}
|
56
|
|
|
|
57
|
3 |
|
$content = curl_exec ($ch);
|
58
|
3 |
|
curl_close ($ch);
|
59
|
3 |
|
return $content;
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
/*
|
63
|
|
|
* Method Post with Upload
|
64
|
|
|
*/
|
65
|
3 |
|
public function post($url, $params = null, $is_upload = false) {
|
66
|
3 |
|
if(!empty($params)){
|
67
|
3 |
|
if (is_array($params) == true) {
|
|
|
|
|
68
|
3 |
|
$query = http_build_query($params);
|
69
|
3 |
|
} else {
|
70
|
|
|
//Raw POST
|
71
|
|
|
$query = $params;
|
72
|
|
|
}
|
73
|
3 |
|
}else{
|
74
|
|
|
$query = '';
|
75
|
|
|
}
|
76
|
3 |
|
$ch = curl_init();
|
77
|
3 |
|
$opts[CURLOPT_URL] = $url;
|
|
|
|
|
78
|
3 |
|
$opts[CURLOPT_RETURNTRANSFER] = 1;
|
79
|
3 |
|
$opts[CURLOPT_CONNECTTIMEOUT] = $this->timeout;
|
80
|
3 |
|
$opts[CURLOPT_USERAGENT] = $this->userAgent;
|
81
|
3 |
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
82
|
|
|
|
83
|
3 |
|
if(!empty($this->cookiePath)){
|
84
|
|
|
$opts[CURLOPT_COOKIEFILE] = $this->cookiePath;
|
85
|
|
|
$opts[CURLOPT_COOKIEJAR] = $this->cookiePath;
|
86
|
|
|
}
|
87
|
|
|
|
88
|
3 |
|
if(!empty($this->headers)) {
|
89
|
|
|
curl_setopt ($ch, CURLOPT_HTTPHEADER, $this->headers);
|
90
|
|
|
}
|
91
|
|
|
|
92
|
3 |
|
if($is_upload){
|
93
|
|
|
$opts[CURLOPT_POSTFIELDS] = $params;
|
94
|
|
|
}else{
|
95
|
3 |
|
$opts[CURLOPT_POSTFIELDS] = $query;
|
96
|
|
|
}
|
97
|
|
|
|
98
|
3 |
|
curl_setopt_array($ch, $opts);
|
99
|
3 |
|
$result = curl_exec ($ch);
|
100
|
3 |
|
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
101
|
3 |
|
curl_close ($ch);
|
102
|
3 |
|
if ($status == 200) {
|
103
|
3 |
|
return $result;
|
104
|
|
|
}
|
105
|
|
|
else {
|
106
|
|
|
return false;
|
107
|
|
|
}
|
108
|
|
|
}
|
109
|
|
|
|
110
|
|
|
/*
|
111
|
|
|
* Download File
|
112
|
|
|
*/
|
113
|
3 |
|
public function download($url, $savePath) {
|
114
|
|
|
|
115
|
3 |
|
$fp = fopen ($savePath, 'w+');
|
116
|
|
|
|
117
|
3 |
|
$ch = curl_init(str_replace(" ","%20",$url));//Here is the file we are downloading, replace spaces with %20
|
118
|
|
|
|
119
|
3 |
|
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
|
120
|
3 |
|
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
|
121
|
3 |
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
122
|
3 |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
123
|
3 |
|
curl_exec($ch); // get curl response
|
124
|
|
|
|
125
|
|
|
|
126
|
3 |
|
$info = curl_getinfo($ch);
|
|
|
|
|
127
|
|
|
|
128
|
3 |
|
fclose($fp);
|
129
|
3 |
|
curl_close($ch);
|
130
|
3 |
|
}
|
131
|
|
|
|
132
|
|
|
public function setHeaders($headers) {
|
133
|
|
|
$this->headers = $headers;
|
134
|
|
|
}
|
135
|
|
|
|
136
|
|
|
public function getHeaders() {
|
137
|
|
|
return $this->headers;
|
138
|
|
|
}
|
139
|
|
|
|
140
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.