27
27
/**
28
28
* Define constants
29
29
*/
30
- $ constants = explode (' ' , 'OBJECT ARRAY ' );
30
+ $ constants = explode (' ' , 'OBJECT ARRAY JSON ' );
31
31
foreach ($ constants as $ i => $ id ) {
32
32
$ id = 'CODEBIRD_RETURNFORMAT_ ' . $ id ;
33
33
defined ($ id ) or define ($ id , $ i );
@@ -351,16 +351,25 @@ public function oauth2_token()
351
351
$ reply = curl_exec ($ ch );
352
352
$ httpstatus = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
353
353
$ reply = $ this ->_parseApiReply ('oauth2/token ' , $ reply );
354
- if ($ this ->_return_format == CODEBIRD_RETURNFORMAT_OBJECT ) {
355
- $ reply ->httpstatus = $ httpstatus ;
356
- if ($ httpstatus == 200 ) {
357
- self ::setBearerToken ($ reply ->access_token );
358
- }
359
- } else {
360
- $ reply ['httpstatus ' ] = $ httpstatus ;
361
- if ($ httpstatus == 200 ) {
362
- self ::setBearerToken ($ reply ['access_token ' ]);
363
- }
354
+ switch ($ this ->_return_format ) {
355
+ case CODEBIRD_RETURNFORMAT_ARRAY :
356
+ $ reply ['httpstatus ' ] = $ httpstatus ;
357
+ if ($ httpstatus == 200 ) {
358
+ self ::setBearerToken ($ reply ['access_token ' ]);
359
+ }
360
+ break ;
361
+ case CODEBIRD_RETURNFORMAT_JSON :
362
+ if ($ httpstatus == 200 ) {
363
+ $ parsed = json_decode ($ reply );
364
+ self ::setBearerToken ($ parsed ->access_token );
365
+ }
366
+ break ;
367
+ case CODEBIRD_RETURNFORMAT_OBJECT :
368
+ $ reply ->httpstatus = $ httpstatus ;
369
+ if ($ httpstatus == 200 ) {
370
+ self ::setBearerToken ($ reply ->access_token );
371
+ }
372
+ break ;
364
373
}
365
374
return $ reply ;
366
375
}
@@ -833,7 +842,7 @@ protected function _callApi($httpmethod, $method, $method_template, $params = ar
833
842
$ reply = $ this ->_parseApiReply ($ method_template , $ reply );
834
843
if ($ this ->_return_format == CODEBIRD_RETURNFORMAT_OBJECT ) {
835
844
$ reply ->httpstatus = $ httpstatus ;
836
- } else {
845
+ } elseif ( $ this -> _return_format == CODEBIRD_RETURNFORMAT_ARRAY ) {
837
846
$ reply ['httpstatus ' ] = $ httpstatus ;
838
847
}
839
848
return $ reply ;
@@ -870,15 +879,17 @@ protected function _parseApiReply($method, $reply)
870
879
871
880
$ need_array = $ this ->_return_format == CODEBIRD_RETURNFORMAT_ARRAY ;
872
881
if ($ reply == '[] ' ) {
873
- return $ need_array ? array () : new \stdClass ;
882
+ switch ($ this ->_return_format ) {
883
+ case CODEBIRD_RETURNFORMAT_ARRAY :
884
+ return array ();
885
+ case CODEBIRD_RETURNFORMAT_JSON :
886
+ return '{} ' ;
887
+ case CODEBIRD_RETURNFORMAT_OBJECT :
888
+ return new \stdClass ;
889
+ }
874
890
}
875
891
$ parsed = array ();
876
- if ($ method == 'users/profile_image/:screen_name ' ) {
877
- // this method returns a 302 redirect, we need to extract the URL
878
- if (isset ($ headers ['Location ' ])) {
879
- $ parsed = array ('profile_image_url_https ' => $ headers ['Location ' ]);
880
- }
881
- } elseif (!$ parsed = json_decode ($ reply , $ need_array )) {
892
+ if (! $ parsed = json_decode ($ reply , $ need_array )) {
882
893
if ($ reply ) {
883
894
if (stripos ($ reply , '< ' . '?xml version="1.0" encoding="UTF-8"? ' . '> ' ) === 0 ) {
884
895
// we received XML...
@@ -901,9 +912,15 @@ protected function _parseApiReply($method, $reply)
901
912
}
902
913
}
903
914
}
904
- }
905
- if (!$ need_array ) {
906
- $ parsed = (object ) $ parsed ;
915
+ $ reply = json_encode ($ parsed );
916
+ }
917
+ switch ($ this ->_return_format ) {
918
+ case CODEBIRD_RETURNFORMAT_ARRAY :
919
+ return $ parsed ;
920
+ case CODEBIRD_RETURNFORMAT_JSON :
921
+ return $ reply ;
922
+ case CODEBIRD_RETURNFORMAT_OBJECT :
923
+ return (object ) $ parsed ;
907
924
}
908
925
return $ parsed ;
909
926
}
0 commit comments