-
Notifications
You must be signed in to change notification settings - Fork 3
/
Curl.php
253 lines (245 loc) · 5.78 KB
/
Curl.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* CodeIgniter Curl Class
*
*
* @package PHP, Phalcon
* @subpackage Class, Libraries
* @category Class, Libraries
* @author Achmad Rozikin
*/
class Curl {
/**
* Declare the private variable for curl option
*/
private $url;
private $postdata;
private $getdata;
private $body;
private $headerdata;
private $userAgent;
private $referer;
private $additionalOpt;
private $requse_method;
private $multi_init_curl;
/**
* Defining the default value of variable
*/
public function __construct(){
$this->clear();
}
/**
* Defining all the default data for all the variable
*/
public function clear(){
$this->url = '';
$this->postdata = [];
$this->getdata = [];
$this->body = null;
$this->headerdata = [];
$this->userAgent = '';
$this->referer = '';
$this->additionalOpt = [];
$this->request_method = '';
$this->multi_init_curl = null;
}
/**
*
* MAIN METHOD OF SET
*
*/
/**
* Set the additional option of curl
*/
public function setOption($opt, $value = 0){
if(is_array($opt)){
foreach($opt as $dataName => $dataValue){
$this->additionalOpt[$dataName] = $dataValue;
}
}else{
$this->additionalOpt[$opt] = $value;
}
}
/**
* Set url of curl
*/
public function setUrl($urlInput){
$this->url = $urlInput;
}
/**
* Set useragentname of curl
*/
public function setUserAgent($userAgentInput){
$this->userAgent= $useragentInput;
}
/**
* Set referer of curl
*/
public function setReferer($refererInput){
$this->referer = $refererInput;
}
/**
* Set postdata of curl to send
*/
public function setPostData($name, $value = 0){
if(is_array($name)){
foreach($name as $dataName => $dataValue):
$this->postdata[$dataName] = $dataValue;
endforeach;
}else{
$this->postdata[$name] = $value;
}
}
/**
* Set getdata of curl to send
*/
public function setGetData($name, $value = 0){
if(is_array($name)){
foreach($name as $dataName => $dataValue):
$this->getdata[$dataName] = $dataValue;
endforeach;
}else{
$this->getdata[$name] = $value;
}
}
/**
* Set header data of curl request
*/
public function setHeaderData($name, $value = 0){
if(is_array($name)){
foreach($name as $dataName => $dataValue):
$this->headerdata[] = $dataName.': '.$dataValue ;
endforeach;
}else{
$this->headerdata[] = $name.': '.$value ;
}
}
/**
* Set the body data without fields array
*/
public function setBody($inp_body){
$this->body = $inp_body;
}
/**
* Set the request method of the curl request
*/
public function setRequestMethod($inp_method){
$this->request_method = $inp_method;
}
/**
*
* MAIN METHOD OF GET
*
*/
/**
* Get the response of curl from given option
*/
public function getResponse(){
$curl = curl_init();
curl_setopt_array($curl, $this->getOptionValue());
$response_curl = curl_exec($curl);
if($response_curl === false){
$response_curl = $this->curlShowError($curl);
}
curl_close($curl);
return $response_curl;
}
/**
* The the instansce of the curl to make it as
*/
public function getInstance(){
$curl = curl_init();
curl_setopt_array($curl, $this->getOptionValue());
return $curl;
}
/**
* Curl multi init add curl instance
*/
public function getResponseMultiInit($inp_curl = []){
$multi_init_curl = curl_multi_init();
foreach($inp_curl as $per_curl){
curl_multi_add_handle($multi_init_curl, $per_curl);
}
$active = null;
// Execute the handles
do{
$mrc = curl_multi_exec($multi_init_curl, $active);
}while($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($multi_init_curl) == -1) {
usleep(100);
}
do {
$mrc = curl_multi_exec($multi_init_curl, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
// Close the handles
foreach($inp_curl as $per_curl){
curl_multi_remove_handle($multi_init_curl, $per_curl);
}
curl_multi_close($multi_init_curl);
}
/**
*
* PRIVATE METHOD TO HANDLE THE OPTIONS VALUES
*
*/
/**
* Get the curl option value
*/
private function getOptionValue(){
$options = $this->additionalOpt;
$options[CURLOPT_RETURNTRANSFER] = 1;
$options[CURLOPT_URL] = $this->url.$this->collectGetData();
if(!empty($this->userAgent)){
$options[CURLOPT_USERAGENT] = $this->userAgent;
}
if(!empty($this->referer)){
$options[CURLOPT_REFERER] = $this->referer;
}
if(!empty($this->headerdata)){
$options[CURLOPT_HTTPHEADER] = $this->headerdata;
}
$postData = $this->collectPostData();
if(!empty($postData)){
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = $postData;
}
if(!empty($this->body)){
$options[CURLOPT_POSTFIELDS] = $this->body;
}
if(!empty($this->request_method)){
$options[CURLOPT_CUSTOMREQUEST] = $this->request_method;
}
return $options;
}
/**
* Get the post data
*/
private function collectPostData(){
$return_data = '';
if(!empty($this->postdata)){
$return_data = $this->postdata;
}
return $return_data;
}
/**
* Get the get data
*/
private function collectGetData(){
$return_data = '';
if(!empty($this->getdata)){
$stringGet = '?';
$stringGet .= http_build_query($this->getdata);
$return_data = $stringGet;
}
return $return_data;
}
/**
* Get the curl error
*/
private function curlShowError($curl){
return 'Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl);
}
}
?>