8000 JSON return format. Fix #21 · codezninja/codebird-php@7f352ba · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7f352ba

Browse files
committed
JSON return format. Fix jublo#21
1 parent 65b855e commit 7f352ba

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ codebird-php - changelog
22
========================
33

44
2.4.0 (not yet released)
5+
+ rfe #21 JSON return format
56

67
2.3.6 (2013-05-12)
78
+ Add backslash to stdClass construction, due to namespace

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ Upon your choice, you may also get PHP arrays directly:
220220
$cb->setReturnFormat(CODEBIRD_RETURNFORMAT_ARRAY);
221221
```
222222

223+
The Twitter API natively responds to API calls in JSON (JS Object Notation).
224+
To get a JSON string, set the corresponding return format:
225+
226+
```php
22 10000 7+
$cb->setReturnFormat(CODEBIRD_RETURNFORMAT_JSON);
228+
```
229+
223230
Support for getting a SimpleXML object is planned.
224231

225232
7. Using multiple Codebird instances

src/codebird.php

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* Define constants
2929
*/
30-
$constants = explode(' ', 'OBJECT ARRAY');
30+
$constants = explode(' ', 'OBJECT ARRAY JSON');
3131
foreach ($constants as $i => $id) {
3232
$id = 'CODEBIRD_RETURNFORMAT_' . $id;
3333
defined($id) or define($id, $i);
@@ -351,16 +351,25 @@ public function oauth2_token()
351351
$reply = curl_exec($ch);
352352
$httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
353353
$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;
364373
}
365374
return $reply;
366375
}
@@ -833,7 +842,7 @@ protected function _callApi($httpmethod, $method, $method_template, $params = ar
833842
$reply = $this->_parseApiReply($method_template, $reply);
834843
if ($this->_return_format == CODEBIRD_RETURNFORMAT_OBJECT) {
835844
$reply->httpstatus = $httpstatus;
836-
} else {
845+
} elseif ($this->_return_format == CODEBIRD_RETURNFORMAT_ARRAY) {
837846
$reply['httpstatus'] = $httpstatus;
838847
}
839848
return $reply;
@@ -870,15 +879,17 @@ protected function _parseApiReply($method, $reply)
870879

871880
$need_array = $this->_return_format == CODEBIRD_RETURNFORMAT_ARRAY;
872881
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+
}
874890
}
875891
$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)) {
882893
if ($reply) {
883894
if (stripos($reply, '<' . '?xml version="1.0" encoding="UTF-8"?' . '>') === 0) {
884895
// we received XML...
@@ -901,9 +912,15 @@ protected function _parseApiReply($method, $reply)
901912
}
902913
}
903914
}
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;
907924
}
908925
return $parsed;
909926
}

0 commit comments

Comments
 (0)
0