8000 Update endpoints list to 1.1 list, add partial support for internal API · robholmes/codebird-php@462ee4b · GitHub
[go: up one dir, main page]

Skip to content

Commit 462ee4b

Browse files
committed
Update endpoints list to 1.1 list, add partial support for internal API
1 parent 0ab6d33 commit 462ee4b

File tree

1 file changed

+134
-85
lines changed

1 file changed

+134
-85
lines changed

codebird.php

Lines changed: 134 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class Codebird
6565
*/
6666
private $_endpoint_oauth = 'https://api.twitter.com/';
6767

68+
/**
69+
* The API endpoint to use for internal requests
70+
*/
71+
private $_endpoint_internal = 'https://api.twitter.com/i/';
72+
6873
/**
6974
* The Request or access token. Used to sign requests
7075
*/
@@ -93,7 +98,7 @@ class Codebird
9398
/**
9499
* The current Codebird version
95100
*/
96-
private $_version = '2.2.1';
101+
private $_version = '2.2.2-internal';
97102

98103
/**
99104
* Returns singleton class instance
@@ -228,13 +233,14 @@ public function __call($fn, $params)
228233
$method_template = str_replace(chr(65 + $i), '_' . chr(97 + $i), $method_template);
229234
}
230235

231-
$httpmethod = $this->_detectMethod($method_template);
236+
$httpmethod = $this->_detectMethod($method_template, $apiparams);
232237
$multipart = $this->_detectMultipart($method_template);
238+
$internal = $this->_detectInternal($method_template);
233239

234240
// geek-geek: Now allowing to specify filenames as params
235241
$this->_detectFilenames($method_template, $apiparams);
236242

237-
return $this->_callApi($httpmethod, $method, $method_template, $apiparams, $multipart);
243+
return $this->_callApi($httpmethod, $method, $method_template, $apiparams, $multipart, $internal);
238244
}
239245

240246
/**
@@ -424,153 +430,166 @@ private function _sign($httpmethod, $method, $params = array(), $multipart = fal
424430
*
425431
* @return string The HTTP method that should be used
426432
*/
427-
private function _detectMethod($method)
433+
private function _detectMethod($method, $params)
428434
{
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',
435+
// multi-HTTP method endpoints
436+
switch($method) {
437+
case 'account/settings':
438+
$method = count($params) > 0 ? $method .= '__post' : $method;
439+
break;
440+
}
441+
442+
$httpmethods = array();
443+
$httpmethods['GET'] = array(
444+
// Timelines
445+
'statuses/mentions_timeline',
437446
'statuses/user_timeline',
438-
'statuses/retweeted_to_user',
439-
'statuses/retweeted_by_user',
447+
'statuses/home_timeline',
448+
440449
// Tweets
441-
'statuses/:id/retweeted_by',
442-
'statuses/:id/retweeted_by/ids',
443450
'statuses/retweets/:id',
444451
'statuses/show/:id',
445452
'statuses/oembed',
453+
454+
// Search
455+
'search/tweets',
456+
446457
// Direct Messages
447458
'direct_messages',
448459
'direct_messages/sent',
449-
'direct_messages/show/:id',
460+
'direct_messages/show',
461+
450462
// Friends & Followers
451-
'followers/ids',
452463
'friends/ids',
453-
'friendships/exists',
464+
'followers/ids',
465+
'friendships/lookup',
454466
'friendships/incoming',
455467
'friendships/outgoing',
456468
'friendships/show',
457-
'friendships/lookup',
458-
'friendships/no_retweet_ids',
469+
459470
// Users
471+
'account/settings',
472+
'account/verify_credentials',
473+
'blocks/list',
474+
'blocks/ids',
460475
'users/lookup',
461-
'users/profile_image/:screen_name',
462-
'users/search',
463476
'users/show',
477+
'users/search',
464478
'users/contributees',
465479
'users/contributors',
480+
466481
// Suggested Users
467-
'users/suggestions',
468482
'users/suggestions/:slug',
483+
'users/suggestions',
469484
'users/suggestions/:slug/members',
485+
470486
// Favorites
471-
'favorites',
487+
'favorites/list',
488+
472489
// Lists
473-
'lists/all',
490+
'lists/list',
474491
'lists/statuses',
475492
'lists/memberships',
476493
'lists/subscribers',
477494
'lists/subscribers/show',
478495
'lists/members/show',
479496
'lists/members',
480-
'lists',
481497
'lists/show',
482498
'lists/subscriptions',
483-
// Accounts
484-
'account/rate_limit_status',
485-
'account/verify_credentials',
486-
'account/totals',
487-
'account/settings',
499+
488500
// Saved searches
489-
'saved_searches',
501+
'saved_searches/list',
490502
'saved_searches/show/:id',
503+
491504
// Places & Geo
492505
'geo/id/:place_id',
493506
'geo/reverse_geocode',
494507
'geo/search',
495508
'geo/similar_places',
509+
496510
// Trends
497-
'trends/:woeid',
511+
'trends/place',
498512
'trends/available',
499-
'trends/daily',
500-
'trends/weekly',
501-
// Block
502-
'blocks/blocking',
503-
'blocks/blocking/ids',
504-
'blocks/exists',
513+
'trends/closest',
514+
505515
// OAuth
506516
'oauth/authenticate',
507517
'oauth/authorize',
518+
508519
// Help
509-
'help/test',
510520
'help/configuration',
511521
'help/languages',
512-
// Legal
513-
'legal/privacy',
514-
'legal/tos'
522+
'help/privacy',
523+
'help/tos',
524+
'application/rate_limit_status',
525+
526+
// Internal
527+
'activity/about_me',
528+
'activity/by_friends',
529+
'search/typeahead',
530+
'statuses/:id/activity/summary'
531+
// Not authorized as of 2012-10-17
532+
// 'discovery',
533+
// 'resolve'
515534
);
516-
$httpmethods['POST'] = array(
517-
// Timeline
535+
$httpmethods['POST'] = array(
536+
// Tweets
518537
'statuses/destroy/:id',
519-
'statuses/retweet/:id',
520538
'statuses/update',
539+
'statuses/retweet/:id',
521540
'statuses/update_with_media',
541+
522542
// Direct Messages
543+
'direct_messages/destroy',
523544
'direct_messages/new',
545+
524546
// Friends & Followers
525547
'friendships/create',
548+
'friendships/destroy',
526549
'friendships/update',
550+
551+
// Users
552+
'account/settings__post',
553+
'account/update_delivery_device',
554+
'account/update_profile',
555+
'account/update_profile_background_image',
556+
'account/update_profile_colors',
557+
'account/update_profile_image',
558+
// not supported in 1.1 as of 2012-10-17
559+
// 'account/update_profile_banner',
560+
// 'account/remove_profile_banner',
561+
'blocks/create',
562+
'blocks/destroy',
563+
527564
// Favorites
528-
'favorites/create/:id',
565+
'favorites/destroy',
566+
'favorites/create',
567+
529568
// Lists
530-
'lists/destroy',
531-
'lists/update',
532-
'lists/create',
533569
'lists/members/destroy',
534-
'lists/members/create_all',
535-
'lists/members/create',
536570
'lists/subscribers/create',
537571
'lists/subscribers/destroy',
538-
// Accounts
539-
'account/end_session',
540-
'account/update_profile',
541-
'account/update_profile_background_image',
542-
'account/update_profile_colors',
543-
'account/update_profile_image',
544-
'account/update_profile_banner',
545-
'account/remove_profile_banner',
546-
'account/settings',
547-
// Notifications
548-
'notifications/follow',
549-
'notifications/leave',
572+
'lists/members/create_all',
573+
'lists/members/create',
574+
'lists/destroy',
575+
'lists/update',
576+
'lists/create',
577+
'lists/members/destroy_all',
578+
550579
// Saved Searches
551580
'saved_searches/create',
581+
'saved_searches/destroy/:id',
582+
552583
// Places & Geo
553584
'geo/place',
554-
// Block
555-
'blocks/create',
585+
556586
// Spam Reporting
557587
'report_spam',
588+
558589
// OAuth
559590
'oauth/access_token',
560591
'oauth/request_token'
561592
);
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-
);
574593
foreach ($httpmethods as $httpmethod => $methods) {
575594
if (in_array($method, $methods)) {
576595
return $httpmethod;
@@ -591,14 +610,36 @@ private function _detectMultipart($method)
591610
$multiparts = array(
592611
// Tweets
593612
'statuses/update_with_media',
594-
// Accounts
613+
614+
// Users
595615
'account/update_profile_background_image',
596616
'account/update_profile_image',
597617
'account/update_profile_banner'
598618
);
599619
return in_array($method, $multiparts);
600620
}
601621

622+
/**
623+
* Detects if API call should use internal endpoint
624+
*
625+
* @param string $method The API method to call
626+
*
627+
* @return bool Whether the method is defined in internal API
628+
*/
629+
private function _detectInternal($method)
630+
{
631+
$internals = array(
632+
// Activity
633+
'activity/about_me',
634+
'activity/by_friends',
635+
'discovery',
636+
'search/typeahead',
637+
'statuses/:id/activity/summary',
638+
'resolve'
639+
);
640+
return in_array($method, $internals);
641+
}
642+
602643
/**
603644
* Detects filenames in upload parameters
604645
*
@@ -670,14 +711,17 @@ private function _detectFilenames($method, &$params)
670711
/**
671712
* Builds the complete API endpoint url
672713
*
673-
* @param string $method The API method to call
714+
* @param string $method The API method to call
715+
* @param string $method_template The API method template to call
674716
*
675717
* @return string The URL to send the request to
676718
*/
677-
private function _getEndpoint($method)
719+
private function _getEndpoint($method, $method_template)
678720
{
679721
if (substr($method, 0, 6) == 'oauth/') {
680722
$url = $this->_endpoint_oauth . $method;
723+
} elseif ($this->_detectInternal($method_template)) {
724+
$url = $this->_endpoint_internal . $method . '.json';
681725
} else {
682726
$url = $this->_endpoint . $method . '.json';
683727
}
@@ -692,16 +736,21 @@ private function _getEndpoint($method)
692736
* @param string $method_template The templated API method to call
693737
* @param array optional $params The parameters to send along
694738
* @param bool optional $multipart Whether to use multipart/form-data
739+
* @param bool optional $internal Whether to use internal API
695740
*
696741
* @return mixed The API reply, encoded in the set return_format
697742
*/
698743

699-
private function _callApi($httpmethod, $method, $method_template, $params = array(), $multipart = false)
744+
private function _callApi($httpmethod, $method, $method_template, $params = array(), $multipart = false, $internal = false)
700745
{
701746
if (! function_exists('curl_init')) {
702747
throw new Exception('To make API requests, the PHP curl extension must be available.');
703748
}
704-
$url = $this->_getEndpoint($method);
749+
if ($internal) {
750+
$params['adc'] = 'phone';
751+
$params['application_id'] = 333903271;
752+
}
753+
$url = $this->_getEndpoint($method, $method_template);
705754
$ch = false;
706755
if ($httpmethod == 'GET') {
707756
$ch = curl_init($this->_sign($httpmethod, $url, $params));
@@ -727,7 +776,7 @@ private function _callApi($httpmethod, $method, $method_template, $params = arra
727776
'Expect:'
728777
));
729778
}
730-
$reply = curl_exec($ch);die($reply);
779+
$reply = curl_exec($ch);
731780
$httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
732781
$reply = $this->_parseApiReply($method_template, $reply);
733782
if ($this->_return_format == CODEBIRD_RETURNFORMAT_OBJECT) {

0 commit comments

Comments
 (0)
0