8000 Update endpoints to 1.1 endpoints, add support for old endpoints · codezninja/codebird-php@d55c73d · GitHub
[go: up one dir, main page]

Skip to content

Commit d55c73d

Browse files
committed
Update endpoints to 1.1 endpoints, add support for old endpoints
2 parents 0ab6d33 + 98d4b66 commit d55c73d

File tree

1 file changed

+108
-82
lines changed

1 file changed

+108
-82
lines changed

codebird.php

Lines changed: 108 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Codebird
9393
/**
9494
* The current Codebird version
9595
*/
96-
private $_version = '2.2.1';
96+
private $_version = '2.2.2';
9797

9898
/**
9999
* Returns singleton class instance
@@ -228,7 +228,7 @@ public function __call($fn, $params)
228228
$method_template = str_replace(chr(65 + $i), '_' . chr(97 + $i), $method_template);
229229
}
230230

231-
$httpmethod = $this->_detectMethod($method_template);
231+
$httpmethod = $this->_detectMethod($method_template, $apiparams);
232232
$multipart = $this->_detectMultipart($method_template);
233233

234234
// geek-geek: Now allowing to specify filenames as params
@@ -424,153 +424,157 @@ private function _sign($httpmethod, $method, $params = array(), $multipart = fal
424424
*
425425
* @return string The HTTP method that should be used
426426
*/
427-
private function _detectMethod($method)
427+
private function _detectMethod($method, $params)
428428
{
429-
$httpmethods = array();
430-
$httpmethods['GET'] = array(
431-
// Timeline
432-
'statuses/home_timeline',
433-
'statuses/mentions',
434-
'statuses/retweeted_by_me',
435-
'statuses/retweeted_to_me',
436-
'statuses/retweets_of_me',
429+
// multi-HTTP method endpoints
430+
switch($method) {
431+
case 'account/settings':
432+
$method = count($params) > 0 ? $method .= '__post' : $method;
433+
break;
434+
}
435+
436+
$httpmethods = array();
437+
$httpmethods['GET'] = array(
438+
// Timelines
439+
'statuses/mentions_timeline',
437440
'statuses/user_timeline',
438-
'statuses/retweeted_to_user',
439-
'statuses/retweeted_by_user',
441+
'statuses/home_timeline',
442+
440443
// Tweets
441-
'statuses/:id/retweeted_by',
442-
'statuses/:id/retweeted_by/ids',
443444
'statuses/retweets/:id',
444445
'statuses/show/:id',
445446
'statuses/oembed',
447+
448+
// Search
449+
'search/tweets',
450+
446451
// Direct Messages
447452
'direct_messages',
448453
'direct_messages/sent',
449-
'direct_messages/show/:id',
454+
'direct_messages/show',
455+
450456
// Friends & Followers
451-
'followers/ids',
452457
'friends/ids',
453-
'friendships/exists',
458+
'followers/ids',
459+
'friendships/lookup',
454460
'friendships/incoming',
455461
'friendships/outgoing',
456462
'friendships/show',
457-
'friendships/lookup',
458-
'friendships/no_retweet_ids',
463+
459464
// Users
465+
'account/settings',
466+
'account/verify_credentials',
467+
'blocks/list',
468+
'blocks/ids',
460469
'users/lookup',
461-
'users/profile_image/:screen_name',
462-
'users/search',
463470
'users/show',
471+
'users/search',
464472
'users/contributees',
465473
'users/contributors',
474+
'users/recommendations',
475+
466476
// Suggested Users
467-
'users/suggestions',
468477
'users/suggestions/:slug',
478+
'users/suggestions',
469479
'users/suggestions/:slug/members',
480+
470481
// Favorites
471-
'favorites',
482+
'favorites/list',
483+
472484
// Lists
473-
'lists/all',
485+
'lists/list',
474486
'lists/statuses',
475487
'lists/memberships',
476488
'lists/subscribers',
477489
'lists/subscribers/show',
478490
'lists/members/show',
479491
'lists/members',
480-
'lists',
481492
'lists/show',
482493
'lists/subscriptions',
483-
// Accounts
484-
'account/rate_limit_status',
485-
'account/verify_credentials',
486-
'account/totals',
487-
'account/settings',
494+
488495
// Saved searches
489-
'saved_searches',
496+
'saved_searches/list',
490497
'saved_searches/show/:id',
498+
491499
// Places & Geo
492500
'geo/id/:place_id',
493501
'geo/reverse_geocode',
494502
'geo/search',
495503
'geo/similar_places',
504+
496505
// Trends
497-
'trends/:woeid',
506+
'trends/place',
498507
'trends/available',
499-
'trends/daily',
500-
'trends/weekly',
501-
// Block
502-
'blocks/blocking',
503-
'blocks/blocking/ids',
504-
'blocks/exists',
508+
'trends/closest',
509+
505510
// OAuth
506511
'oauth/authenticate',
507512
'oauth/authorize',
513+
508514
// Help
509-
'help/test',
510515
'help/configuration',
511516
'help/languages',
512-
// Legal
513-
'legal/privacy',
514-
'legal/tos'
517+
'help/privacy',
518+
'help/tos',
519+
'application/rate_limit_status'
515520
);
516-
$httpmethods['POST'] = array(
517-
// Timeline
521+
$httpmethods['POST'] = array(
522+
// Tweets
518523
'statuses/destroy/:id',
519-
'statuses/retweet/:id',
520524
'statuses/update',
525+
'statuses/retweet/:id',
521526
'statuses/update_with_media',
527+
522528
// Direct Messages
529+
'direct_messages/destroy',
523530
'direct_messages/new',
531+
524532
// Friends & Followers
525533
'friendships/create',
534+
'friendships/destroy',
526535
'friendships/update',
527-
// Favorites
528-
'favorites/create/:id',
529-
// Lists
530-
'lists/destroy',
531-
'lists/update',
532-
'lists/create',
533-
'lists/members/destroy',
534-
'lists/members/create_all',
535-
'lists/members/create',
536-
'lists/subscribers/create',
537-
'lists/subscribers/destroy',
538-
// Accounts
539-
'account/end_session',
536+
537+
// Users
538+
'account/settings__post',
539+
'account/update_delivery_device',
540540
'account/update_profile',
541541
'account/update_profile_background_image',
542542
'account/update_profile_colors',
543543
'account/update_profile_image',
544544
'account/update_profile_banner',
545545
'account/remove_profile_banner',
546-
'account/settings',
547-
// Notifications
548-
'notifications/follow',
549-
'notifications/leave',
546+
'blocks/create',
547+
'blocks/destroy',
548+
549+
// Favorites
550+
'favorites/destroy',
551+
'favorites/create',
552+
553+
// Lists
554+
'lists/members/destroy',
555+
'lists/subscribers/create',
556+
'lists/subscribers/destroy',
557+
'lists/members/create_all',
558+
'lists/members/create',
559+
'lists/destroy',
560+
'lists/update',
561+
'lists/create',
562+
'lists/members/destroy_all',
563+
550564
// Saved Searches
551565
'saved_searches/create',
566+
'saved_searches/destroy/:id',
567+
552568
// Places & Geo
553569
'geo/place',
554-
// Block
555-
'blocks/create',
570+
556571
// Spam Reporting
557572
'report_spam',
573+
558574
// OAuth
559575
'oauth/access_token',
560576
'oauth/request_token'
561577 );
562-
$httpmethods['DELETE'] = array(
563-
// Direct Messages
564-
'direct_messages/destroy/:id',
565-
// Friends & Followers
566-
'friendships/destroy',
567-
// Favorites
568-
'favorites/destroy/:id',
569-
// Saved Searches
570-
'saved_searches/destroy/:id',
571-
// Block
572-
'blocks/destroy'
573-
);
574578
foreach ($httpmethods as $httpmethod => $methods) {
575579
if (in_array($method, $methods)) {
576580
return $httpmethod;
@@ -591,14 +595,33 @@ private function _detectMultipart($method)
591595
$multiparts = array(
592596
// Tweets
593597
'statuses/update_with_media',
594-
// Accounts
598+
599+
// Users
595600
'account/update_profile_background_image',
596601
'account/update_profile_image',
597602
'account/update_profile_banner'
598603
);
599604
return in_array($method, $multiparts);
600605
}
601606

607+
/**
608+
* Detects if API call should use the old endpoint
609+
*
610+
* @param string $method The API method to call
611+
*
612+
* @return bool Whether the method is defined in old API
613+
*/
614+
private function _detectOld($method)
615+
{
616+
$olds = array(
617+
// Users
618+
'account/update_profile_banner',
619+
'account/remove_profile_banner',
620+
'users/recommendations'
621+
);
622+
return in_array($method, $olds);
623+
}
624+
602625
/**
603626
* Detects filenames in upload parameters
604627
*
@@ -670,14 +693,17 @@ private function _detectFilenames($method, &$params)
670693
/**
671694
* Builds the complete API endpoint url
672695
*
673-
* @param string $method The API method to call
696+
* @param string $method The API method to call
697+
* @param string $method_template The API method template to call
674698
*
675699
* @return string The URL to send the request to
676700
*/
677-
private function _getEndpoint($method)
701+
private function _getEndpoint($method, $method_template)
678702
{
679703
if (substr($method, 0, 6) == 'oauth/') {
680704
$url = $this->_endpoint_oauth . $method;
705+
} elseif ($this->_detectOld($method_template)) {
706+
$url = $this->_endpoint_old . $method . '.json';
681707
} else {
682708
$url = $this->_endpoint . $method . '.json';
683709
}
@@ -701,7 +727,7 @@ private function _callApi($httpmethod, $method, $method_template, $params = arra
701727
if (! function_exists('curl_init')) {
702728
throw new Exception('To make API requests, the PHP curl extension must be available.');
703729
}
704-
$url = $this->_getEndpoint($method);
730+
$url = $this->_getEndpoint($method, $method_template);
705731
$ch = false;
706732
if ($httpmethod == 'GET') {
707733
$ch = curl_init($this->_sign($httpmethod, $url, $params));
@@ -727,7 +753,7 @@ private function _callApi($httpmethod, $method, $method_template, $params = arra
727753
'Expect:'
728754
));
729755
}
730-
$reply = curl_exec($ch);die($reply);
756+
$reply = curl_exec($ch);
731757
$httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
732758
$reply = $this->_parseApiReply($method_template, $reply);
733759
if ($this->_return_format == CODEBIRD_RETURNFORMAT_OBJECT) {

0 commit comments

Comments
 (0)
0