@@ -16,8 +16,6 @@ class TwitterOAuth
16
16
{
17
17
protected $ url = 'https://api.twitter.com/1.1/ ' ;
18
18
19
- protected $ format = 'json ' ;
20
-
21
19
protected $ config = array ();
22
20
23
21
protected $ call = '' ;
@@ -28,6 +26,8 @@ class TwitterOAuth
28
26
29
27
protected $ postParams = array ();
30
28
29
+ protected $ arrayOutput = false ;
30
+
31
31
32
32
/**
33
33
* Prepare a new conection with Twitter API via OAuth
@@ -55,20 +55,21 @@ public function __construct(array $config)
55
55
/**
56
56
* Send a GET call to Twitter API via OAuth
57
57
*
58
- * @params string $call Twitter resource string
59
- * @params array $getParams GET parameters to send
60
- * @params string $format Set the response format
58
+ * @param $call Twitter resource string
59
+ * @param array $getParams GET parameters to send
60
+ * @param bool $arrayOutput Output format (false = Object | true = Array)
61
+ * @return mixed Output with selected format
61
62
*/
62
- public function get ($ call , array $ getParams = null , $ format = null )
63
+ public function get ($ call , array $ getParams = null , $ arrayOutput = false )
63
64
{
64
65
$ this ->call = $ call ;
65
66
66
67
if ($ getParams !== null && is_array ($ getParams )) {
67
68
$ this ->getParams = $ getParams ;
68
69
}
69
70
70
- if ($ format !== null ) {
71
- $ this ->format = $ format ;
71
+ if ($ arrayOutput !== false ) {
72
+ $ this ->arrayOutput = true ;
72
73
}
73
74
74
75
return $ this ->sendRequest ();
@@ -77,12 +78,13 @@ public function get($call, array $getParams = null, $format = null)
77
78
/**
78
79
* Send a POST call to Twitter API via OAuth
79
80
*
80
- * @params string $call Twitter resource string
81
- * @params array $postParams POST parameters to send
82
- * @params array $getParams GET parameters to send
83
- * @params string $format Set the response format
81
+ * @param $call Twitter resource string
82
+ * @param array $postParams POST parameters to send
83
+ * @param array $getParams GET parameters to send
84
+ * @param bool $arrayOutput Output format (false = Object | true = Array)
85
+ * @return mixed Output with selected format
84
86
*/
85
- public function post ($ call , array $ postParams = null , array $ getParams = null , $ format = null )
87
+ public function post ($ call , array $ postParams = null , array $ getParams = null , $ arrayOutput = false )
86
88
{
87
89
$ this ->call = $ call ;
88
90
@@ -96,8 +98,8 @@ public function post($call, array $postParams = null, array $getParams = null, $
96
98
$ this ->getParams = $ getParams ;
97
99
}
98
100
99
- if ($ format !== null ) {
100
- $ this ->format = $ format ;
101
+ if ($ arrayOutput !== false ) {
102
+ $ this ->arrayOutput = true ;
101
103
}
102
104
103
105
return $ this ->sendRequest ();
@@ -125,7 +127,7 @@ protected function getParams(array $params)
125
127
}
126
128
127
129
/**
128
- * Getting full URL from a Twitter resource and format
130
+ * Getting full URL from a Twitter resource
129
131
*
130
132
* @param bool $withParams If true then parameters will be outputted
131
133
* @return string Full URL
@@ -142,7 +144,7 @@ protected function getUrl($withParams = false)
142
144
}
143
145
}
144
146
145
- return $ this ->url . $ this ->call . '. ' . $ this -> format . $ getParams ;
147
+ return $ this ->url . $ this ->call . '.json ' . $ getParams ;
146
148
}
147
149
148
150
/**
@@ -253,7 +255,7 @@ protected function buildRequestHeader()
253
255
* Send GET or POST requests to Twitter API
254
256
*
255
257
* @throws Exception\TwitterException
256
- * @return mixed Response output with the selected format
258
+ * @return mixed Response output
257
259
*/
258
260
protected function sendRequest ()
259
261
{
@@ -284,12 +286,20 @@ protected function sendRequest()
284
286
285
287
unset($ options , $ c );
286
288
287
- $ response = json_decode ($ response );
289
+ if (!in_array ($ response [0 ], array ('{ ' , '[ ' ))) {
290
+ throw new TwitterException (str_replace (array ("\n" , "\r" , "\t" ), '' , strip_tags ($ response )), 0 );
291
+ }
288
292
289
- if (isset ($ response ->errors )) {
290
- foreach ($ response ->errors as $ error ) {
293
+ $ response = json_decode ($ response , $ this ->arrayOutput );
294
+
295
+ if (!$ this ->arrayOutput && isset ($ response ->errors )) {
296
+ foreach ($ response ->errors as $ error ) {
291
297
throw new TwitterException ($ error ->message , $ error ->code );
292
298
}
299
+ } elseif ($ this ->arrayOutput && isset ($ response ['errors ' ])) {
300
+ foreach ($ response ['errors ' ] as $ error ) {
301
+ throw new TwitterException ($ error ['message ' ], $ error ['code ' ]);
302
+ }
293
303
}
294
304
295
305
return $ response ;
0 commit comments