@@ -17,6 +17,27 @@ class RestfulService extends ViewableData {
17
17
protected $ customHeaders = array ();
18
18
protected $ proxy ;
19
19
protected static $ default_proxy ;
20
+ protected static $ default_curl_options = array ();
21
+
22
+ /**
23
+ * set a curl option that will be applied to all requests as default
24
+ * {@see http://php.net/manual/en/function.curl-setopt.php#refsect1-function.curl-setopt-parameters}
25
+ *
26
+ * @param int $option The cURL opt Constant
27
+ * @param mixed $value The cURL opt value
28
+ */
29
+ public static function set_default_curl_option ($ option , $ value ) {
30
+ self ::$ default_curl_options [$ option ] = $ value ;
31
+ }
32
+
33
+ /**
34
+ * set many defauly curl options at once
35
+ */
36
+ public static function set_default_curl_options ($ optionArray ) {
37
+ foreach ($ optionArray as $ option => $ value ) {
38
+ self ::set_default_curl_option ($ option , $ value );
39
+ }
40
+ }
20
41
21
42
/**
22
43
* Sets default proxy settings for outbound RestfulService connections
@@ -119,7 +140,7 @@ public function request($subURL = '', $method = "GET", $data = null, $headers =
119
140
$ method ,
120
141
$ data ,
121
142
array_merge ((array )$ this ->customHeaders , (array )$ headers ),
122
- $ curlOptions ,
143
+ array_merge ( self :: $ default_curl_options , $ curlOptions) ,
123
144
$ this ->getBasicAuthString ()
124
145
));
125
146
@@ -175,6 +196,7 @@ public function curlRequest($url, $method, $data = null, $headers = null, $curlO
175
196
$ timeout = 5 ;
176
197
$ sapphireInfo = new SapphireInfo ();
177
198
$ useragent = 'SilverStripe/ ' . $ sapphireInfo ->Version ();
199
+ $ curlOptions = array_merge (self ::$ default_curl_options , $ curlOptions );
178
200
179
201
curl_setopt ($ ch , CURLOPT_URL , $ url );
180
202
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
@@ -183,7 +205,6 @@ public function curlRequest($url, $method, $data = null, $headers = null, $curlO
183
205
if (!ini_get ('open_basedir ' )) curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION ,1 );
184
206
curl_setopt ($ ch , CURLOPT_CUSTOMREQUEST , $ method );
185
207
//include headers in the response
186
- //curl_setopt($ch, CURLOPT_VERBOSE, true);
187
208
curl_setopt ($ ch , CURLOPT_HEADER , true );
188
209
189
210
// Add headers
@@ -220,17 +241,9 @@ public function curlRequest($url, $method, $data = null, $headers = null, $curlO
220
241
221
242
// Run request
222
243
$ rawResponse = curl_exec ($ ch );
223
- $ curlError = curl_error ($ ch );
224
- $ responseHeaders = array ();
225
- $ responseBody = '' ;
226
- $ this ->extractResponse ($ ch , $ rawResponse , $ responseBody , $ responseHeaders );
227
-
228
- $ statusCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
229
- if ($ curlError !== '' || $ statusCode == 0 ) $ statusCode = 500 ;
244
+ $ response = $ this ->extractResponse ($ ch , $ rawResponse );
230
245
curl_close ($ ch );
231
246
232
- $ response = new RestfulService_Response ($ responseBody , $ statusCode , $ responseHeaders );
233
-
234
247
return $ response ;
235
248
}
236
249
@@ -279,22 +292,31 @@ protected function getCachePath($cacheData) {
279
292
*
280
293
* @param curl_handle $ch The curl handle for the request
281
294
* @param string $rawResponse The raw response text
282
- * @param string &$body the body text
283
- * @param array &headers The header array
295
+ *
296
+ * @return RestfulService_Response The response object
284
297
*/
285
- protected function extractResponse ($ ch , $ rawResponse , &$ body , &$ headers ) {
298
+ protected function extractResponse ($ ch , $ rawResponse ) {
299
+ //get the status code
300
+ $ statusCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
301
+ //normalise the status code
302
+ if ($ curlError !== '' || $ statusCode == 0 ) $ statusCode = 500 ;
303
+ //calculate the length of the header and extract it
286
304
$ headerLength = curl_getinfo ($ ch , CURLINFO_HEADER_SIZE );
287
305
$ rawHeaders = substr ($ rawResponse , 0 , $ headerLength );
306
+ //extract the body
288
307
$ body = substr ($ rawResponse , $ headerLength );
289
- $ headers = self ::parse_raw_headers ($ rawHeaders );
308
+ //parse the headers
309
+ $ headers = $ this ->parseRawHeaders ($ rawHeaders );
310
+ //return the response object
311
+ return new RestfulService_Response ($ body , $ statusCode , $ headers );
290
312
}
291
313
292
314
/**
293
315
* Takes raw headers and parses them to turn them to an associative array
294
316
*
295
317
* Any header that we see more than once is turned into an array.
296
318
*
297
- * This is meant to mimic htt_parse_headers {@link http://php.net/manual/en/function.http-parse-headers.php}
319
+ * This is meant to mimic http_parse_headers {@link http://php.net/manual/en/function.http-parse-headers.php}
298
320
* thanks to comment #77241 on that page for foundation of this
299
321
*
300
322
* @param string $rawHeaders The raw header string
@@ -315,12 +337,12 @@ protected function parseRawHeaders($rawHeaders) {
315
337
$ headers [$ match [1 ]] = array ($ headers [$ match [1 ]]);
316
338
}
317
339
$ headers [$ match [1 ]][] = $ match [2 ];
318
- } else {
319
- $ headers [$ match [1 ]] = trim ($ match [2 ]);
320
- }
321
- }
322
- }
323
- return $ headers ;
340
+ } else {
341
+ $ headers [$ match [1 ]] = trim ($ match [2 ]);
342
+ }
343
+ }
344
+ }
345
+ return $ headers ;
324
346
}
325
347
326
348
@@ -514,7 +536,7 @@ class RestfulService_Response extends SS_HTTPResponse {
514
536
protected $ simpleXML ;
515
537
516
538
/**
517
- * @var boolean It should be populated with cached content
539
+ * @var boolean It should be populated with cached request
518
540
* when a request referring to this response was unsuccessful
519
541
*/
520
542
protected $ cachedResponse = false ;
@@ -556,6 +578,19 @@ public function getCachedBody() {
556
578
}
557
579
return false ;
558
580
}
581
+
582
+ /**
583
+ * @param string
584
+ */
585
+ public function setCachedBody ($ content ) {
586
+ Deprecation::notice ('3.1 ' , 'Setting the response body is now deprecated, set the cached request instead ' );
587
+ if (!$ this ->cachedResponse ) {
588
+ $ this ->cachedResponse = new RestfulService_Response ($ content );
589
+ }
590
+ else {
591
+ $ this ->cachedResponse ->setBody = $ content ;
592
+ }
593
+ }
559
594
560
595
/**
561
596
* @param string
0 commit comments