-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.php
129 lines (113 loc) · 3.47 KB
/
Request.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
128
129
<?php
namespace Util;
Class Request {
public function __construct() {
$this->cookieFile = sprintf('%s/tmp/cookies', __DIR__);
$this->userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0';
}
/**
* Remove cookieFile if exists
*/
public function __destruct() {
if (file_exists($this->cookieFile)) {
unlink($this->cookieFile);
}
}
/**
* GET request
* @param string $url Url to submit to request via GET method
* @param callable $callback Callback function to call after request
* @return void
*/
public function getRequest($url, \Closure $callback = null) {
$ch = curl_init($url);
curl_setopt_array(
$ch,
[
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEJAR => $this->cookieFile,
CURLOPT_COOKIEFILE => $this->cookieFile,
CURLOPT_USERAGENT => $this->userAgent,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
]
);
$this->httpResponse = curl_exec($ch);
$this->httpInfo = curl_getinfo($ch);
curl_close($ch);
if (null !== $callback) {
call_user_func_array($callback, [$this->httpResponse, $this->httpInfo]);
return;
}
return $this;
}
/**
* POST request
* @param string $url Url to submit to request via POST methods
* @param array $payload Post payload
* @param callable $callback Callback function to call after request
* @return void
*/
public function postRequest($url, array $payload, \Closure $callback = null) {
$ch = curl_init($url);
curl_setopt_array(
$ch,
[
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEJAR => $this->cookieFile,
CURLOPT_COOKIEFILE => $this->cookieFile,
CURLOPT_USERAGENT => $this->userAgent,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($payload),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
]
);
$this->httpResponse = curl_exec($ch);
$this->httpInfo = curl_getinfo($ch);
curl_close($ch);
if (null !== $callback) {
call_user_func_array($callback, [$this->httpResponse, $this->httpInfo]);
return;
}
return $this;
}
/**
* Get body raw from last response
* @return string
*/
public function getHttpResponse() {
return $this->httpResponse;
}
/**
* Get info about last response
* @return [type] [description]
*/
public function getHttpInfo() {
return $this->httpInfo;
}
/**
* Cookie file
* @var string
*/
protected $cookieFile;
/**
* User agent
* @var string
*/
protected $userAgent;
/**
* Http response
* @var string
*/
protected $httpResponse;
/**
* Http info
* @var array
*/
protected $httpInfo;
// HTTP responses
CONST HTTP_OK = 200; // OK
CONST HTTP_REDIRECT = 302; // REDIRECT
CONST HTTP_NOT_FOUND = 404; // NOT FOUND
}