Skip to content

Commit

Permalink
JSON return format. Fix jublo#21
Browse files Browse the repository at this point in the history
  • Loading branch information
mynetx committed Jun 1, 2013
1 parent 65b855e commit 7f352ba
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ codebird-php - changelog
========================

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

2.3.6 (2013-05-12)
+ Add backslash to stdClass construction, due to namespace
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ Upon your choice, you may also get PHP arrays directly:
$cb->setReturnFormat(CODEBIRD_RETURNFORMAT_ARRAY);
```

The Twitter API natively responds to API calls in JSON (JS Object Notation).
To get a JSON string, set the corresponding return format:

```php
$cb->setReturnFormat(CODEBIRD_RETURNFORMAT_JSON);
```

Support for getting a SimpleXML object is planned.

7. Using multiple Codebird instances
Expand Down
61 changes: 39 additions & 22 deletions src/codebird.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* Define constants
*/
$constants = explode(' ', 'OBJECT ARRAY');
$constants = explode(' ', 'OBJECT ARRAY JSON');
foreach ($constants as $i => $id) {
$id = 'CODEBIRD_RETURNFORMAT_' . $id;
defined($id) or define($id, $i);
Expand Down Expand Up @@ -351,16 +351,25 @@ public function oauth2_token()
$reply = curl_exec($ch);
$httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$reply = $this->_parseApiReply('oauth2/token', $reply);
if ($this->_return_format == CODEBIRD_RETURNFORMAT_OBJECT) {
$reply->httpstatus = $httpstatus;
if ($httpstatus == 200) {
self::setBearerToken($reply->access_token);
}
} else {
$reply['httpstatus'] = $httpstatus;
if ($httpstatus == 200) {
self::setBearerToken($reply['access_token']);
}
switch ($this->_return_format) {
case CODEBIRD_RETURNFORMAT_ARRAY:
$reply['httpstatus'] = $httpstatus;
if ($httpstatus == 200) {
self::setBearerToken($reply['access_token']);
}
break;
case CODEBIRD_RETURNFORMAT_JSON:
if ($httpstatus == 200) {
$parsed = json_decode($reply);
self::setBearerToken($parsed->access_token);
}
break;
case CODEBIRD_RETURNFORMAT_OBJECT:
$reply->httpstatus = $httpstatus;
if ($httpstatus == 200) {
self::setBearerToken($reply->access_token);
}
break;
}
return $reply;
}
Expand Down Expand Up @@ -833,7 +842,7 @@ protected function _callApi($httpmethod, $method, $method_template, $params = ar
$reply = $this->_parseApiReply($method_template, $reply);
if ($this->_return_format == CODEBIRD_RETURNFORMAT_OBJECT) {
$reply->httpstatus = $httpstatus;
} else {
} elseif ($this->_return_format == CODEBIRD_RETURNFORMAT_ARRAY) {
$reply['httpstatus'] = $httpstatus;
}
return $reply;
Expand Down Expand Up @@ -870,15 +879,17 @@ protected function _parseApiReply($method, $reply)

$need_array = $this->_return_format == CODEBIRD_RETURNFORMAT_ARRAY;
if ($reply == '[]') {
return $need_array ? array() : new \stdClass;
switch ($this->_return_format) {
case CODEBIRD_RETURNFORMAT_ARRAY:
return array();
case CODEBIRD_RETURNFORMAT_JSON:
return '{}';
case CODEBIRD_RETURNFORMAT_OBJECT:
return new \stdClass;
}
}
$parsed = array();
if ($method == 'users/profile_image/:screen_name') {
// this method returns a 302 redirect, we need to extract the URL
if (isset($headers['Location'])) {
$parsed = array('profile_image_url_https' => $headers['Location']);
}
} elseif (!$parsed = json_decode($reply, $need_array)) {
if (! $parsed = json_decode($reply, $need_array)) {
if ($reply) {
if (stripos($reply, '<' . '?xml version="1.0" encoding="UTF-8"?' . '>') === 0) {
// we received XML...
Expand All @@ -901,9 +912,15 @@ protected function _parseApiReply($method, $reply)
}
}
}
}
if (!$need_array) {
$parsed = (object) $parsed;
$reply = json_encode($parsed);
}
switch ($this->_return_format) {
case CODEBIRD_RETURNFORMAT_ARRAY:
return $parsed;
case CODEBIRD_RETURNFORMAT_JSON:
return $reply;
case CODEBIRD_RETURNFORMAT_OBJECT:
return (object) $parsed;
}
return $parsed;
}
Expand Down

0 comments on commit 7f352ba

Please sign in to comment.