8000 Add support for users/profile_image/:screen_name, drop support for st… · codezninja/codebird-php@fec4faf · GitHub
[go: up one dir, main page]

Skip to content

Commit fec4faf

Browse files
committed
Add support for users/profile_image/:screen_name, drop support for string return format
1 parent 73666b0 commit fec4faf

File tree

2 files changed

+99
-37
lines changed

2 files changed

+99
-37
lines changed

README.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ map to Codebird function calls. The general rules are:
119119
3. For each parameter template in method, use UPPERCASE in the Codebird function.
120120
Also don’t forget to include the parameter in your parameter list.
121121

122-
Example: ```statuses/show/:id``` maps to ```Codebird::statuses_show_ID('id=12345')```.
122+
Examples:
123+
- ```statuses/show/:id``` maps to ```Codebird::statuses_show_ID('id=12345')```.
124+
- ```users/profile_image/:screen_name``` maps to
125+
```Codebird::users_profileImage_SCREEN_NAME('screen_name=mynetx')```.
123126

124127
HTTP methods (GET, POST, DELETE etc.)
125128
-------------------------------------
@@ -150,12 +153,6 @@ Upon your choice, you may also get PHP arrays directly:
150153
$cb->setReturnFormat(CODEBIRD_RETURNFORMAT_ARRAY);
151154
```
152155

153-
Finally, if you prefer seeing an URL-encoded string as reply, use this:
154-
155-
```php
156-
$cb->setReturnFormat(CODEBIRD_RETURNFORMAT_STRING);
157-
```
158-
159156
Using multiple Codebird instances
160157
---------------------------------
161158

@@ -180,3 +177,32 @@ $cb2 = new Codebird;
180177
Please note that your OAuth consumer key and secret is shared within
181178
multiple Codebird instances, while the OAuth request and access tokens with their
182179
secrets are *not* shared.
180+
181+
Specialities
182+
============
183+
184+
Accessing a user’s profile image
185+
--------------------------------
186+
187+
The Twitter API usually contains data in either JSON or XML. However, the
188+
templated method ```users/profile_image/:screen_name``` uses a HTTP 302 redirect
189+
to send you to the requested image file URL.
190+
191+
Codebird intercepts this HTTP redirect and extracts the profile image URL instead.
192+
Thus, the following API call:
193+
194+
```php
195+
$reply = $cb->users_profileImage_SCREEN_NAME('screen_name=mynetx&size=mini');
196+
```
197+
198+
returns an object with the following contents:
199+
```
200+
stdClass Object
201+
(
202+
[profile_image_url_https] => https://si0.twimg.com/profile_images/1417135246/Blue_Purple.96_mini.png
203+
[httpstatus] => 302
204+
)
205+
```
206+
207+
You can find out how to build the Codebird method name, in the section
208+
‘Mapping API methods to Codebird function calls.’

codebird.php

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/**
2525
* Define constants
2626
*/
27-
$constants = explode(' ', 'OBJECT ARRAY STRING');
27+
$constants = explode(' ', 'OBJECT ARRAY');
2828
foreach ($constants as $i => $id) {
2929
$id = 'CODEBIRD_RETURNFORMAT_' . $id;
3030
defined($id) or define($id, $i);
@@ -99,7 +99,7 @@ class Codebird
9999
/**
100100
* The current Codebird version
101101
*/
102-
private $_version = '2.1.3007.0035';
102+
private $_version = '2.2.0';
103103

104104
/**
105105
* Returns singleton class instance
@@ -159,7 +159,6 @@ public function setToken($token, $secret)
159159
* @param int $return_format One of these:
160160
* CODEBIRD_RETURNFORMAT_OBJECT (default)
161161
* CODEBIRD_RETURNFORMAT_ARRAY
162-
* CODEBIRD_RETURNFORMAT_STRING
163162
*
164163
* @return void
165164
*/
@@ -200,36 +199,49 @@ public function __call($fn, $params)
200199
}
201200
$method .= $path[$i];
202201
}
202+
// undo replacement for URL parameters
203+
$url_parameters_with_underscore = array('screen_name');
204+
foreach ($url_parameters_with_underscore as $param) {
205+
$param = strtoupper($param);
206+
$replacement_was = str_replace('_', '/', $param);
207+
$method = str_replace($replacement_was, $param, $method);
208+
}
203209

204210
// replace AA by URL parameters
205-
$method2 = $method;
211+
$method_template = $method;
206212
$match = array();
207-
if (preg_match('/[A-Z]{2,}/', $method, $match)) {
213+
if (preg_match('/[A-Z_]{2,}/', $method, $match)) {
208214
foreach ($match as $param) {
209215
$param_l = strtolower($param);
216+
$method_template = str_replace($param, ':' . $param_l, $method_template);
210217
if (!isset($apiparams[$param_l])) {
211-
throw new Exception('To call the templated method "' . $method . '", specify the parameter value for "' . $param_l . '".');
218+
for ($i = 0; $i < 26; $i++) {
219+
$method_template = str_replace(chr(65 + $i), '_' . chr(97 + $i), $method_template);
220+
}
221+
throw new Exception(
222+
'To call the templated method "' . $method_template
223+
. '", specify the parameter value for "' . $param_l . '".'
224+
);
212225
}
213226
$method = str_replace($param, $apiparams[$param_l], $method);
214-
$method2 = str_replace($param, ':' . $param_l, $method2);
215227
unset($apiparams[$param_l]);
216228
}
217229
}
218230

219231
// replace A-Z by _a-z
220232
for ($i = 0; $i < 26; $i++) {
221233
$method = str_replace(chr(65 + $i), '_' . chr(97 + $i), $method);
222-
$method2 = str_replace(chr(65 + $i), '_' . chr(97 + $i), $method2);
234+
$method_template = str_replace(chr(65 + $i), '_' . chr(97 + $i), $method_template);
223235
}
224236

225-
$httpmethod = $this->_detectMethod($method2);
226-
$sign = $this->_detectSign($method2);
227-
$multipart = $this->_detectMultipart($method2);
237+
$httpmethod = $this->_detectMethod($method_template);
238+
$sign = $this->_detectSign($method_template);
239+
$multipart = $this->_detectMultipart($method_template);
228240

229241
// geek-geek: Now allowing to specify filenames as params
230-
$this->_detectFilenames($method2, $apiparams);
242+
$this->_detectFilenames($method_template, $apiparams);
231243

232-
return $this->_callApi($httpmethod, $method, $apiparams, $sign, $multipart);
244+
return $this->_callApi($httpmethod, $method, $method_template, $apiparams, $sign, $multipart);
233245
}
234246

235247
/**
@@ -700,16 +712,17 @@ private function _getEndpoint($method)
700712
/**
701713
* Calls the API using cURL
702714
*
703-
* @param string $httpmethod The HTTP method to use for making the request
704-
* @param string $method The API method to call
705-
* @param array optional $params The parameters to send along
706-
* @param bool optional $sign Whether to sign the API call
707-
* @param bool optional $multipart Whether to use multipart/form-data
715+
* @param string $httpmethod The HTTP method to use for making the request
716+
* @param string $method The API method to call
717+
* @param string $method_template The templated API method to call
718+
* @param array optional $params The parameters to send along
719+
* @param bool optional $sign Whether to sign the API call
720+
* @param bool optional $multipart Whether to use multipart/form-data
708721
*
< 10000 /code>
709722
* @return mixed The API reply, encoded in the set return_format
710723
*/
711724

712-
private function _callApi($httpmethod, $method, $params = array(), $sign = true, $multipart = false)
725+
private function _callApi($httpmethod, $method, $method_template, $params = array(), $sign = true, $multipart = false)
713726
{
714727
if ($sign && !isset($this->_oauth_token)) {
715728
throw new Exception('To make a signed API request, the OAuth token must be set.');
@@ -734,7 +747,7 @@ private function _callApi($httpmethod, $method, $params = array(), $sign = true,
734747
}
735748
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
736749
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
737-
curl_setopt($ch, CURLOPT_HEADER, 0);
750+
curl_setopt($ch, CURLOPT_HEADER, 1);
738751
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
739752
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
740753
if (isset($authorization)) {
@@ -745,32 +758,55 @@ private function _callApi($httpmethod, $method, $params = array(), $sign = true,
745758
}
746759
$reply = curl_exec($ch);
747760
$httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
748-
if ($this->_return_format != CODEBIRD_RETURNFORMAT_STRING) {
749-
$reply = $this->_parseApiReply($reply);
750-
if ($this->_return_format == CODEBIRD_RETURNFORMAT_OBJECT) {
751-
$reply->httpstatus = $httpstatus;
752-
} else {
753-
$reply['httpstatus'] = $httpstatus;
754-
}
761+
$reply = $this->_parseApiReply($method_template, $reply);
762+
if ($this->_return_format == CODEBIRD_RETURNFORMAT_OBJECT) {
763+
$reply->httpstatus = $httpstatus;
764+
} else {
765+
$reply['httpstatus'] = $httpstatus;
755766
}
756767
return $reply;
757768
}
758769

759770
/**
760771
* Parses the API reply to encode it in the set return_format
761772
*
762-
* @param string $reply The actual reply, JSON-encoded or URL-encoded
773+
* @param string $method The method that has been called
774+
* @param string $reply The actual reply, JSON-encoded or URL-encoded
763775
*
764776
* @return array|object The parsed reply
765777
*/
766-
private function _parseApiReply($reply)
778+
private function _parseApiReply($method, $reply)
767779
{
780+
// split headers and body
781+
$headers = array();
782+
$reply = explode("\r\n\r\n", $reply, 2);
783+
$headers_array = explode("\r\n", $reply[0]);
784+
foreach ($headers_array as $header) {
785+
$header_array = explode(': ', $header, 2);
786+
$key = $header_array[0];
787+
$value = '';
788+
if (count($header_array) > 1) {
789+
$value = $header_array[1];
790+
}
791+
$headers[$key] = $value;
792+
}
793+
if (count($reply) > 1) {
794+
$reply = $reply[1];
795+
} else {
796+
$reply = '';
797+
}
798+
768799
$need_array = $this->_return_format == CODEBIRD_RETURNFORMAT_ARRAY;
769800
if ($reply == '[]') {
770801
return $need_array ? array() : new stdClass;
771802
}
772803
$parsed = array();
773-
if (!$parsed = json_decode($reply, $need_array)) {
804+
if ($method == 'users/profile_image/:screen_name') {
805+
// this method returns a 302 redirect, we need to extract the URL
806+
if (isset($headers['Location'])) {
807+
$parsed = array('profile_image_url_https' => $headers['Location']);
808+
}
809+
} elseif (!$parsed = json_decode($reply, $need_array)) {
774810
if ($reply) {
775811
$reply = explode('&', $reply);
776812
foreach ($reply as $element) {

0 commit comments

Comments
 (0)
0