24
24
/**
25
25
* Define constants
26
26
*/
27
- $ constants = explode (' ' , 'OBJECT ARRAY STRING ' );
27
+ $ constants = explode (' ' , 'OBJECT ARRAY ' );
28
28
foreach ($ constants as $ i => $ id ) {
29
29
$ id = 'CODEBIRD_RETURNFORMAT_ ' . $ id ;
30
30
defined ($ id ) or define ($ id , $ i );
@@ -99,7 +99,7 @@ class Codebird
99
99
/**
100
100
* The current Codebird version
101
101
*/
102
- private $ _version = '2.1.3007.0035 ' ;
102
+ private $ _version = '2.2.0 ' ;
103
103
104
104
/**
105
105
* Returns singleton class instance
@@ -159,7 +159,6 @@ public function setToken($token, $secret)
159
159
* @param int $return_format One of these:
160
160
* CODEBIRD_RETURNFORMAT_OBJECT (default)
161
161
* CODEBIRD_RETURNFORMAT_ARRAY
162
- * CODEBIRD_RETURNFORMAT_STRING
163
162
*
164
163
* @return void
165
164
*/
@@ -200,36 +199,49 @@ public function __call($fn, $params)
200
199
}
201
200
$ method .= $ path [$ i ];
202
201
}
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
+ }
203
209
204
210
// replace AA by URL parameters
205
- $ method2 = $ method ;
211
+ $ method_template = $ method ;
206
212
$ match = array ();
207
- if (preg_match ('/[A-Z ]{2,}/ ' , $ method , $ match )) {
213
+ if (preg_match ('/[A-Z_ ]{2,}/ ' , $ method , $ match )) {
208
214
foreach ($ match as $ param ) {
209
215
$ param_l = strtolower ($ param );
216
+ $ method_template = str_replace ($ param , ': ' . $ param_l , $ method_template );
210
217
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
+ );
212
225
}
213
226
$ method = str_replace ($ param , $ apiparams [$ param_l ], $ method );
214
- $ method2 = str_replace ($ param , ': ' . $ param_l , $ method2 );
215
227
unset($ apiparams [$ param_l ]);
216
228
}
217
229
}
218
230
219
231
// replace A-Z by _a-z
220
232
for ($ i = 0 ; $ i < 26 ; $ i ++) {
221
233
$ 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 );
223
235
}
224
236
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 );
228
240
229
241
// geek-geek: Now allowing to specify filenames as params
230
- $ this ->_detectFilenames ($ method2 , $ apiparams );
242
+ $ this ->_detectFilenames ($ method_template , $ apiparams );
231
243
232
- return $ this ->_callApi ($ httpmethod , $ method , $ apiparams , $ sign , $ multipart );
244
+ return $ this ->_callApi ($ httpmethod , $ method , $ method_template , $ apiparams , $ sign , $ multipart );
233
245
}
234
246
235
247
/**
@@ -700,16 +712,17 @@ private function _getEndpoint($method)
700
712
/**
701
713
* Calls the API using cURL
702
714
*
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
708
721
*
<
10000
/code>
709
722
* @return mixed The API reply, encoded in the set return_format
710
723
*/
711
724
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 )
713
726
{
714
727
if ($ sign && !isset ($ this ->_oauth_token )) {
715
728
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,
734
747
}
735
748
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
736
749
curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , 0 );
737
- curl_setopt ($ ch , CURLOPT_HEADER , 0 );
750
+ curl_setopt ($ ch , CURLOPT_HEADER , 1 );
738
751
curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , 0 );
739
752
curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 0 );
740
753
if (isset ($ authorization )) {
@@ -745,32 +758,55 @@ private function _callApi($httpmethod, $method, $params = array(), $sign = true,
745
758
}
746
759
$ reply = curl_exec ($ ch );
747
760
$ 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 ;
755
766
}
756
767
return $ reply ;
757
768
}
758
769
759
770
/**
760
771
* Parses the API reply to encode it in the set return_format
761
772
*
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
763
775
*
764
776
* @return array|object The parsed reply
765
777
*/
766
- private function _parseApiReply ($ reply )
778
+ private function _parseApiReply ($ method , $ reply )
767
779
{
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
+
768
799
$ need_array = $ this ->_return_format == CODEBIRD_RETURNFORMAT_ARRAY ;
769
800
if ($ reply == '[] ' ) {
770
801
return $ need_array ? array () : new stdClass ;
771
802
}
772
803
$ 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 )) {
774
810
if ($ reply ) {
775
811
$ reply = explode ('& ' , $ reply );
776
812
foreach ($ reply as $ element ) {
0 commit comments