1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* spindle/httpclient |
4
|
|
|
* @license CC0-1.0 (Public Domain) https://creativecommons.org/publicdomain/zero/1.0/ |
5
|
|
|
* @see https://github.com/spindle/spindle-httpclient |
6
|
|
|
*/ |
7
|
|
|
namespace Spindle\HttpClient; |
8
|
|
|
|
9
|
|
|
class Request |
10
|
|
|
{ |
11
|
|
|
private $responseClass = '\Spindle\HttpClient\Response'; |
12
|
|
|
private $handle; |
13
|
|
|
|
14
|
|
|
protected $options = array( |
15
|
|
|
'returnTransfer' => true, |
16
|
|
|
'header' => true, |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
protected $response; |
20
|
|
|
|
21
|
|
|
protected $error; |
22
|
|
|
|
23
|
8 |
|
function __construct($url = null, array $options = array()) { |
|
|
|
|
24
|
8 |
|
if (is_string($url)) { |
25
|
5 |
|
$this->handle = curl_init($url); |
26
|
5 |
|
$options['url'] = $url; |
27
|
5 |
|
} else { |
28
|
4 |
|
$this->handle = curl_init(); |
29
|
|
|
} |
30
|
|
|
|
31
|
8 |
|
$this->options += $options; |
32
|
8 |
|
$this->setOptions($this->options); |
33
|
8 |
|
} |
34
|
|
|
|
35
|
7 |
|
function __destruct() { |
|
|
|
|
36
|
7 |
|
curl_close($this->handle); |
37
|
7 |
|
} |
38
|
|
|
|
39
|
1 |
|
function __clone() { |
|
|
|
|
40
|
1 |
|
$this->handle = curl_copy_handle($this->handle); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
8 |
|
function setOptions(array $options) { |
|
|
|
|
44
|
8 |
|
$this->options = $options + $this->options; |
45
|
8 |
|
curl_setopt_array($this->handle, $this->_toCurlSetopt($options)); |
46
|
8 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
function setOption($label, $val) { |
|
|
|
|
50
|
3 |
|
$this->options[$label] = $val; |
51
|
3 |
|
curl_setopt($this->handle, $this->_toCurlOption($label), $val); |
52
|
1 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
function getOption($label) { |
|
|
|
|
56
|
1 |
|
return $this->options[$label]; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
function getOptions() { |
|
|
|
|
60
|
1 |
|
return $this->options; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
function getHandle() |
|
|
|
|
64
|
|
|
{ |
65
|
2 |
|
return $this->handle; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
* @throws CurlException |
71
|
|
|
*/ |
72
|
2 |
|
function send() { |
|
|
|
|
73
|
2 |
|
$body = curl_exec($this->handle); |
74
|
2 |
|
$info = curl_getinfo($this->handle); |
75
|
2 |
|
$this->response = $res = new $this->responseClass($body, $info); |
76
|
|
|
|
77
|
2 |
|
$errno = curl_errno($this->handle); |
78
|
2 |
|
if (0 !== $errno) { |
79
|
1 |
|
$err = new CurlException(curl_error($this->handle), $errno); |
80
|
1 |
|
$err->setRequest($this); |
81
|
1 |
|
$this->error = $err; |
82
|
1 |
|
throw $err; |
83
|
|
|
} |
84
|
1 |
|
return $res; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
function setResponse($res) { |
|
|
|
|
88
|
1 |
|
$this->response = $res; |
89
|
1 |
|
} |
90
|
|
|
|
91
|
2 |
|
function getResponse() { |
|
|
|
|
92
|
2 |
|
return $this->response; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
function setError(CurlException $err) { |
|
|
|
|
96
|
|
|
$this->error = $err; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
function getError() { |
|
|
|
|
100
|
|
|
return $this->error; |
101
|
|
|
} |
102
|
|
|
|
103
|
8 |
|
protected function _toCurlSetopt(array $optionList) { |
104
|
8 |
|
$fixedOptionList = array(); |
105
|
8 |
|
foreach ($optionList as $opt => $value) { |
106
|
8 |
|
$label = $this->_toCurlOption($opt); |
107
|
8 |
|
$fixedOptionList[$label] = $value; |
108
|
8 |
|
} |
109
|
8 |
|
return $fixedOptionList; |
110
|
|
|
} |
111
|
|
|
|
112
|
8 |
|
protected function _toCurlOption($label) { |
113
|
8 |
|
if (is_int($label)) { |
114
|
1 |
|
return $label; |
115
|
|
|
} |
116
|
|
|
|
117
|
8 |
|
if (is_string($label)) { |
118
|
8 |
|
$const = 'CURLOPT_' . strtoupper($label); |
119
|
8 |
|
if (defined($const)) { |
120
|
8 |
|
$curlopt = constant($const); |
121
|
8 |
|
} else { |
122
|
1 |
|
throw new \InvalidArgumentException("$label does not exist in CURLOPT_* constants."); |
123
|
|
|
} |
124
|
8 |
|
return $curlopt; |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
throw new \InvalidArgumentException('label is invalid'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.