Skip to content

Commit d121866

Browse files
committed
Added Multi Output Formats (text|json|array|object)
1 parent 303c659 commit d121866

File tree

3 files changed

+173
-39
lines changed

3 files changed

+173
-39
lines changed

Example.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
require_once __DIR__ . '/TwitterOAuth/TwitterOAuth.php';
4+
require_once __DIR__ . '/TwitterOAuth/Exception/TwitterException.php';
5+
6+
7+
use TwitterOAuth\TwitterOAuth;
8+
9+
date_default_timezone_set('UTC');
10+
11+
12+
/**
13+
* Array with the OAuth tokens provided by Twitter when you create application
14+
*
15+
* output_format - Optional - Values: text|json|array|object - Default: object
16+
*/
17+
$config = array(
18+
'consumer_key' => '01b307acba4f54f55aafc33bb06bbbf6ca803e9a',
19+
'consumer_secret' => '926ca39b94e44e5bdd4a8705604b994ca64e1f72',
20+
'oauth_token' => 'e98c603b55646a6d22249d9b0096e9af29bafcc2',
21+
'oauth_token_secret' => '07cfdf42835998375e71b46d96b4488a5c659c2f',
22+
'output_format' => 'object'
23+
);
24+
25+
/**
26+
* Instantiate TwitterOAuth class with set tokens
27+
*/
28+
$tw = new TwitterOAuth($config);
29+
30+
31+
/**
32+
* Returns a collection of the most recent Tweets posted by the user
33+
* https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
34+
*/
35+
$params = array(
36+
'screen_name' => 'ricard0per',
37+
'count' => 5,
38+
'exclude_replies' => true
39+
);
40+
41+
/**
42+
* Send a GET call with set parameters
43+
*/
44+
$response = $tw->get('statuses/user_timeline', $params);
45+
46+
var_dump($response);
47+
48+
49+
/**
50+
* Creates a new list for the authenticated user
51+
* https://dev.twitter.com/docs/api/1.1/post/lists/create
52+
*/
53+
$params = array(
54+
'name' => 'TwOAuth',
55+
'mode' => 'private',
56+
'description' => 'Test List',
57+
);
58+
59+
/**
60+
* Send a POST call with set parameters
61+
*/
62+
$response = $tw->post('lists/create', $params);
63+
64+
var_dump($response);

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ The recommended way to install TwitterOAuth is through [Composer](http://getcomp
3737

3838
/**
3939
* Array with the OAuth tokens provided by Twitter when you create application
40+
*
41+
* output_format - Optional - Values: text|json|array|object - Default: object
4042
*/
41-
$config = array(
42-
'consumer_key' => '01b307acba4f54f55aafc33bb06bbbf6ca803e9a',
43-
'consumer_secret' => '926ca39b94e44e5bdd4a8705604b994ca64e1f72',
44-
'oauth_token' => 'e98c603b55646a6d22249d9b0096e9af29bafcc2',
45-
'oauth_token_secret' => '07cfdf42835998375e71b46d96b4488a5c659c2f'
46-
);
43+
$config = array(
44+
'consumer_key' => '01b307acba4f54f55aafc33bb06bbbf6ca803e9a',
45+
'consumer_secret' => '926ca39b94e44e5bdd4a8705604b994ca64e1f72',
46+
'oauth_token' => 'e98c603b55646a6d22249d9b0096e9af29bafcc2',
47+
'oauth_token_secret' => '07cfdf42835998375e71b46d96b4488a5c659c2f',
48+
'output_format' => 'object'
49+
);
4750

4851
/**
4952
* Instantiate TwitterOAuth class with set tokens
@@ -56,7 +59,7 @@ The recommended way to install TwitterOAuth is through [Composer](http://getcomp
5659
* https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
5760
*/
5861
$params = array(
59-
'screen_name' => 'username',
62+
'screen_name' => 'ricard0per',
6063
'count' => 5,
6164
'exclude_replies' => true
6265
);

TwitterOAuth/TwitterOAuth.php

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

19+
protected $outputFormats = array('text', 'json', 'array', 'object');
20+
21+
protected $defaultFormat = 'object';
22+
1923
protected $config = array();
2024

2125
protected $call = '';
@@ -26,8 +30,6 @@ class TwitterOAuth
2630

2731
protected $postParams = array();
2832

29-
protected $arrayOutput = false;
30-
3133

3234
/**
3335
* Prepare a new conection with Twitter API via OAuth
@@ -36,55 +38,53 @@ class TwitterOAuth
3638
*/
3739
public function __construct(array $config)
3840
{
39-
$keys = array(
41+
$required = array(
4042
'consumer_key' => '',
4143
'consumer_secret' => '',
4244
'oauth_token' => '',
4345
'oauth_token_secret' => ''
4446
);
4547

46-
if (count(array_intersect_key($keys, $config)) !== count($keys)) {
48+
if (count(array_intersect_key($required, $config)) !== count($required)) {
4749
throw new \Exception('Missing parameters in configuration array');
4850
}
4951

52+
if (!isset($config['output_format']) || !in_array($config['output_format'], $this->outputFormats)) {
53+
$config['output_format'] = $this->defaultFormat;
54+
}
55+
5056
$this->config = $config;
5157

52-
unset($keys, $config);
58+
unset($required, $config);
5359
}
5460

5561
/**
5662
* Send a GET call to Twitter API via OAuth
5763
*
58-
* @param $call Twitter resource string
64+
* @param string $call Twitter resource string
5965
* @param array $getParams GET parameters to send
60-
* @param bool $arrayOutput Output format (false = Object | true = Array)
6166
* @return mixed Output with selected format
6267
*/
63-
public function get($call, array $getParams = null, $arrayOutput = false)
68+
public function get($call, array $getParams = null)
6469
{
6570
$this->call = $call;
6671

6772
if ($getParams !== null && is_array($getParams)) {
6873
$this->getParams = $getParams;
6974
}
7075

71-
if ($arrayOutput !== false) {
72-
$this->arrayOutput = true;
73-
}
74-
7576
return $this->sendRequest();
7677
}
7778

7879
/**
7980
* Send a POST call to Twitter API via OAuth
8081
*
81-
* @param $call Twitter resource string
82+
* @param string $call Twitter resource string
8283
* @param array $postParams POST parameters to send
8384
* @param array $getParams GET parameters to send
84-
* @param bool $arrayOutput Output format (false = Object | true = Array)
8585
* @return mixed Output with selected format
8686
*/
87-
public function post($call, array $postParams = null, array $getParams = null, $arrayOutput = false)
87+
public function post($call, array $postParams = null, array $getParams = null)
8888
{
8989
$this->call = $call;
9090

@@ -98,10 +98,6 @@ public function post($call, array $postParams = null, array $getParams = null, $
9898
$this->getParams = $getParams;
9999
}
100100

101-
if ($arrayOutput !== false) {
102-
$this->arrayOutput = true;
103-
}
104-
105101
return $this->sendRequest();
106102
}
107103

@@ -251,6 +247,89 @@ protected function buildRequestHeader()
251247
);
252248
}
253249

250+
/**
251+
* Processing Twitter Exceptions in case of error
252+
*
253+
* @param string $type Depends of response format (array|object)
254+
* @param mixed $ex Exceptions
255+
* @throws Exception\TwitterException
256+
*/
257+
protected function processExceptions($type, $ex)
258+
{
259+
switch ($type) {
260+
case 'array':
261+
foreach ($ex['errors'] as $error) {
262+
throw new TwitterException($error['message'], $error['code']);
263+
}
264+
265+
break;
266+
267+
default:
268+
foreach ($ex->errors as $error) {
269+
throw new TwitterException($error->message, $error->code);
270+
}
271+
}
272+
273+
unset($tupe, $ex, $error);
274+
}
275+
276+
/**
277+
* Outputs the response in the selected format
278+
*
279+
* @param string $response
280+
* @return mixed
281+
*/
282+
protected function processOutput($response)
283+
{
284+
$format = $this->config['output_format'];
285+
286+
switch ($format) {
287+
case 'text':
288+
if (substr($response, 2, 6) == 'errors') {
289+
$response = json_decode($response);
290+
291+
$this->processExceptions('object', $response);
292+
}
293+
294+
break;
295+
296+
case 'json':
297+
if (!headers_sent()) {
298+
header('Cache-Control: no-cache, must-revalidate');
299+
header('Expires: Tue, 19 May 1981 00:00:00 GMT');
300+
header('Content-type: application/json');
301+
}
302+
303+
if (substr($response, 2, 6) == 'errors') {
304+
$response = json_decode($response);
305+
306+
$this->processExceptions('object', $response);
307+
}
308+
309+
break;
310+
311+
case 'array':
312+
$response = json_decode($response, true);
313+
314+
if (isset($response['errors'])) {
315+
$this->processExceptions('array', $response);
316+
}
317+
318+
break;
319+
320+
default:
321+
$response = json_decode($response);
322+
323+
if (isset($response->errors)) {
324+
$this->processExceptions('object', $response);
325+
}
326+
}
327+
328+
unset($format);
329+
330+
return $response;
331+
}
332+
254333
/**
255334
* Send GET or POST requests to Twitter API
256335
*
@@ -290,18 +369,6 @@ protected function sendRequest()
290369
throw new TwitterException(str_replace(array("\n", "\r", "\t"), '', strip_tags($response)), 0);
291370
}
292371

293-
$response = json_decode($response, $this->arrayOutput);
294-
295-
if (!$this->arrayOutput && isset($response->errors)) {
296-
foreach ($response->errors as $error) {
297-
throw new TwitterException($error->message, $error->code);
298-
}
299-
} elseif ($this->arrayOutput && isset($response['errors'])) {
300-
foreach ($response['errors'] as $error) {
301-
throw new TwitterException($error['message'], $error['code']);
302-
}
303-
}
304-
305-
return $response;
372+
return $this->processOutput($response);
306373
}
307374
}

0 commit comments

Comments
 (0)