1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ©[2016] SugarCRM Inc. Licensed by SugarCRM under the Apache 2.0 license. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace SugarAPI\SDK\Request\Abstracts; |
7
|
|
|
|
8
|
|
|
use SugarAPI\SDK\Request\Interfaces\RequestInterface; |
9
|
|
|
|
10
|
|
|
abstract class AbstractRequest implements RequestInterface { |
11
|
|
|
|
12
|
|
|
const STATUS_INIT = 'initialized'; |
13
|
|
|
const STATUS_SENT = 'sent'; |
14
|
|
|
const STATUS_CLOSED = 'closed'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The HTTP Request Type |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected static $_TYPE = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The Default Curl Options |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected static $_DEFAULT_OPTIONS = array( |
27
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0, |
28
|
|
|
CURLOPT_HEADER => TRUE, |
29
|
|
|
CURLOPT_SSL_VERIFYPEER => 0, |
30
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
31
|
|
|
CURLOPT_FOLLOWLOCATION => 0, |
32
|
|
|
CURLOPT_USERAGENT => 'SugarAPI-SDK-PHP' |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The default HTTP Headers to be added to Curl Request |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected static $_DEFAULT_HEADERS = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The Curl Resource used to actually send data to Sugar API |
43
|
|
|
* @var - Curl Resource |
44
|
|
|
*/ |
45
|
|
|
protected $CurlResponse; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The raw response from curl_exec |
49
|
|
|
* @var - Curl Response |
50
|
|
|
*/ |
51
|
|
|
protected $CurlRequest; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* List of Headers for Request |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $headers = array(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The body of the request or payload. JSON Encoded |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $body = ''; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The URL the Request is sent to |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $url = ''; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var null |
73
|
|
|
*/ |
74
|
|
|
protected $status = NULL; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The Request Type |
78
|
|
|
* @var |
79
|
|
|
*/ |
80
|
|
|
protected $type; |
81
|
|
|
|
82
|
|
|
public function __construct($url = NULL){ |
83
|
|
|
$this->start(); |
84
|
|
|
if (!empty($url)){ |
85
|
|
|
$this->setURL($url); |
86
|
|
|
} |
87
|
|
|
$this->setType(); |
88
|
|
|
foreach (static::$_DEFAULT_OPTIONS as $option => $value){ |
89
|
|
|
$this->setOption($option, $value); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Always make sure to destroy Curl Resource |
95
|
|
|
*/ |
96
|
|
|
public function __destruct() { |
97
|
|
|
if ($this->status!==self::STATUS_CLOSED){ |
98
|
|
|
curl_close($this->CurlRequest); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
105
|
|
|
public function setURL($url){ |
106
|
|
|
$this->url = $url; |
107
|
|
|
$this->setOption(CURLOPT_URL, $this->url); |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritdoc |
113
|
|
|
*/ |
114
|
|
|
public function getURL(){ |
115
|
|
|
return $this->url; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritdoc |
120
|
|
|
*/ |
121
|
|
|
public function addHeader($name, $value){ |
122
|
|
|
$token = $name.": ".$value; |
123
|
|
|
$this->headers[] = $token; |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritdoc |
129
|
|
|
*/ |
130
|
|
|
public function setHeaders(array $array = array()){ |
131
|
|
|
if (count($array)>0){ |
132
|
|
|
foreach ($array as $key => $value){ |
133
|
|
|
$this->addHeader($key, $value); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
$this->setOption(CURLOPT_HTTPHEADER, $this->headers); |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritdoc |
142
|
|
|
*/ |
143
|
|
|
public function getHeaders(){ |
144
|
|
|
return $this->headers; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @inheritdoc |
149
|
|
|
*/ |
150
|
|
|
public function setBody($body){ |
151
|
|
|
$this->body = $body; |
152
|
|
|
$this->setOption(CURLOPT_POSTFIELDS, $this->body); |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritdoc |
158
|
|
|
*/ |
159
|
|
|
public function getBody(){ |
160
|
|
|
return $this->body; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @inheritdoc |
165
|
|
|
*/ |
166
|
|
|
public function getCurlObject(){ |
167
|
|
|
return $this->CurlRequest; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritdoc |
172
|
|
|
*/ |
173
|
|
|
public function setOption($option, $value){ |
174
|
|
|
curl_setopt($this->CurlRequest, $option, $value); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @inheritdoc |
179
|
|
|
*/ |
180
|
|
|
public function send(){ |
181
|
|
|
$this->setHeaders(); |
182
|
|
|
$this->CurlResponse = curl_exec($this->CurlRequest); |
183
|
|
|
$this->status = self::STATUS_SENT; |
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @inheritdoc |
189
|
|
|
*/ |
190
|
|
|
public function getCurlResponse(){ |
191
|
|
|
return $this->CurlResponse; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Set the Type on the Request |
196
|
|
|
*/ |
197
|
|
|
protected function setType(){ |
198
|
|
|
$this->type = static::$_TYPE; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @inheritdoc |
203
|
|
|
*/ |
204
|
|
|
public function getType(){ |
205
|
|
|
return $this->type; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @inheritdoc |
210
|
|
|
*/ |
211
|
|
|
public function reset(){ |
212
|
|
|
if (is_object($this->CurlRequest)){ |
213
|
|
|
$this->close(); |
214
|
|
|
} |
215
|
|
|
$this->start(); |
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @inheritdoc |
221
|
|
|
*/ |
222
|
|
|
public function start(){ |
223
|
|
|
$this->CurlRequest = curl_init(); |
224
|
|
|
$this->status = self::STATUS_INIT; |
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @inheritdoc |
230
|
|
|
*/ |
231
|
|
|
public function close(){ |
232
|
|
|
curl_close($this->CurlRequest); |
233
|
|
|
unset($this->CurlRequest); |
234
|
|
|
$this->status = self::STATUS_CLOSED; |
|
|
|
|
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @inheritdoc |
240
|
|
|
*/ |
241
|
|
|
public function getCurlStatus(){ |
242
|
|
|
return $this->status; |
243
|
|
|
} |
244
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..