Skip to content

Commit 18e37d8

Browse files
committed
Added output format (Object or Array) + Improvements in Exception Class and in the error messages
1 parent f3e2b45 commit 18e37d8

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

TwitterOAuth/Exception/TwitterException.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
* @author Dirk Luijk <dirk@luijkwebcreations.nl>
88
* @copyright 2013
99
*/
10-
11-
namespace TwitterOAuth\Exception;
12-
1310

14-
class TwitterException extends \Exception {
11+
namespace TwitterOAuth\Exception;
1512

13+
class TwitterException extends \Exception
14+
{
15+
public function __toString()
16+
{
17+
return "Twitter API Response: [{$this->code}] {$this->message} (" . __CLASS__ . ") ";
18+
}
1619
}

TwitterOAuth/TwitterOAuth.php

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class TwitterOAuth
1616
{
1717
protected $url = 'https://api.twitter.com/1.1/';
1818

19-
protected $format = 'json';
20-
2119
protected $config = array();
2220

2321
protected $call = '';
@@ -28,6 +26,8 @@ class TwitterOAuth
2826

2927
protected $postParams = array();
3028

29+
protected $arrayOutput = false;
30+
3131

3232
/**
3333
* Prepare a new conection with Twitter API via OAuth
@@ -55,20 +55,21 @@ public function __construct(array $config)
5555
/**
5656
* Send a GET call to Twitter API via OAuth
5757
*
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
6162
*/
62-
public function get($call, array $getParams = null, $format = null)
63+
public function get($call, array $getParams = null, $arrayOutput = false)
6364
{
6465
$this->call = $call;
6566

6667
if ($getParams !== null && is_array($getParams)) {
6768
$this->getParams = $getParams;
6869
}
6970

70-
if ($format !== null) {
71-
$this->format = $format;
71+
if ($arrayOutput !== false) {
72+
$this->arrayOutput = true;
7273
}
7374

7475
return $this->sendRequest();
@@ -77,12 +78,13 @@ public function get($call, array $getParams = null, $format = null)
7778
/**
7879
* Send a POST call to Twitter API via OAuth
7980
*
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
8486
*/
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)
8688
{
8789
$this->call = $call;
8890

@@ -96,8 +98,8 @@ public function post($call, array $postParams = null, array $getParams = null, $
9698
$this->getParams = $getParams;
9799
}
98100

99-
if ($format !== null) {
100-
$this->format = $format;
101+
if ($arrayOutput !== false) {
102+
$this->arrayOutput = true;
101103
}
102104

103105
return $this->sendRequest();
@@ -125,7 +127,7 @@ protected function getParams(array $params)
125127
}
126128

127129
/**
128-
* Getting full URL from a Twitter resource and format
130+
* Getting full URL from a Twitter resource
129131
*
130132
* @param bool $withParams If true then parameters will be outputted
131133
* @return string Full URL
@@ -142,7 +144,7 @@ protected function getUrl($withParams = false)
142144
}
143145
}
144146

145-
return $this->url . $this->call . '.' . $this->format . $getParams;
147+
return $this->url . $this->call . '.json' . $getParams;
146148
}
147149

148150
/**
@@ -253,7 +255,7 @@ protected function buildRequestHeader()
253255
* Send GET or POST requests to Twitter API
254256
*
255257
* @throws Exception\TwitterException
256-
* @return mixed Response output with the selected format
258+
* @return mixed Response output
257259
*/
258260
protected function sendRequest()
259261
{
@@ -284,12 +286,20 @@ protected function sendRequest()
284286

285287
unset($options, $c);
286288

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+
}
288292

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) {
291297
throw new TwitterException($error->message, $error->code);
292298
}
299+
} elseif ($this->arrayOutput && isset($response['errors'])) {
300+
foreach ($response['errors'] as $error) {
301+
throw new TwitterException($error['message'], $error['code']);
302+
}
293303
}
294304

295305
return $response;

0 commit comments

Comments
 (0)