@@ -17,6 +17,7 @@ class Curl
17
17
private $ error_function = null ;
18
18
private $ complete_function = null ;
19
19
20
+ private $ json_decoder = null ;
20
21
private $ json_pattern = '~^application/(?:json|vnd\.api\+json)~i ' ;
21
22
private $ xml_pattern = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i ' ;
22
23
@@ -50,6 +51,7 @@ public function __construct()
50
51
51
52
$ this ->curl = curl_init ();
52
53
$ this ->setDefaultUserAgent ();
54
+ $ this ->setDefaultJsonDecoder ();
53
55
$ this ->setOpt (CURLINFO_HEADER_OUT , true );
54
56
$ this ->setOpt (CURLOPT_RETURNTRANSFER , true );
55
57
$ this ->headers = new CaseInsensitiveArray ();
@@ -241,6 +243,20 @@ public function setDefaultUserAgent()
241
243
$ this ->setUserAgent ($ user_agent );
242
244
}
243
245
246
+ public function setDefaultJsonDecoder () {
247
+ $ this ->json_decoder = function ($ response ) {
248
+ $ json_obj = json_decode ($ response , false );
249
+ if (!($ json_obj === null )) {
250
+ $ response = $ json_obj ;
251
+ }
252
+ return $ response ;
253
+ };
254
+ }
255
+
256
+ public function setJsonDecoder ($ func ) {
257
+ $ this ->json_decoder = $ func ;
258
+ }
259
+
244
260
public function setUserAgent ($ user_agent )
245
261
{
246
262
$ this ->setOpt (CURLOPT_USERAGENT , $ user_agent );
@@ -370,14 +386,11 @@ private function parseRequestHeaders($raw_headers)
370
386
371
387
private function parseResponse ($ response_headers , $ raw_response )
372
388
{
373
-
374
389
$ response = $ raw_response ;
375
390
if (isset ($ response_headers ['Content-Type ' ])) {
376
391
if (preg_match ($ this ->json_pattern , $ response_headers ['Content-Type ' ])) {
377
- $ json_obj = json_decode ($ response , false );
378
- if ($ json_obj !== null ) {
379
- $ response = $ json_obj ;
380
- }
392
+ $ json_decoder = $ this ->json_decoder ;
393
+ $ response = $ json_decoder ($ response );
381
394
} elseif (preg_match ($ this ->xml_pattern , $ response_headers ['Content-Type ' ])) {
382
395
$ xml_obj = @simplexml_load_string ($ response );
383
396
if (!($ xml_obj === false )) {
0 commit comments