-
Notifications
You must be signed in to change notification settings - Fork 1
/
Curl.php
83 lines (60 loc) · 2.06 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
<?
class Curl extends CurlClient{
public $response = null;
public $http_code = null;
public $error = null;
private $url = null;
public $options = [];
public function POST($url = '', $header = '', $data = ''){
$this->setUrl($url);
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'POST');
$this->setOpt(CURLOPT_POST, TRUE);
if($data != '')
$this->setOpt(CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
if($header != '')
$this->setOpt(CURLOPT_HTTPHEADER, $header);
$this->doRequest();
}
public function GET($url = '', $header = ''){
$this->setUrl($url);
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
if($header != '')
$this->setOpt(CURLOPT_HTTPHEADER, $header);
$this->doRequest();
}
public function DELETE($url = '', $header = ''){
}
public function PUT($url = '', $header = ''){
}
public function PATCH($url = '', $header = ''){
}
}
class CurlClient{
public $maxRedirs = 10;
public $timeout = 30;
public $curlHttpVersion = 'CURL_HTTP_VERSION_1_1';
protected function doRequest(){
$curl = curl_init($this->url);
$this->setDefaultOptions();
curl_setopt_array($curl, $this->options);
$this->response = curl_exec($curl);
$this->http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$this->error = curl_error($curl);
curl_close($curl);
}
public function setOpt($option_name, $option_value){
$this->options[$option_name] = $option_value;
return $this;
}
protected function setDefaultOptions(){
$this->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
$this->setOpt(CURLOPT_MAXREDIRS, $this->maxRedirs);
$this->setOpt(CURLOPT_TIMEOUT, $this->timeout);
$this->setOpt(CURLOPT_HTTP_VERSION, $this->curlHttpVersion);
}
protected function setUrl($url){
$this->url = $url;
return $this;
}
}
?>