]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitteroauthclient.php
c798ac8771d83d2a70c7a780b24d51b6b2a8d3a1
[quix0rs-gnu-social.git] / lib / twitteroauthclient.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Class for doing OAuth calls against Twitter
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Integration
23  * @package   Laconica
24  * @author    Zach Copley <zach@controlyourself.ca>
25  * @copyright 2008 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * Class for talking to the Twitter API with OAuth.
36  *
37  * @category Integration
38  * @package  Laconica
39  * @author   Zach Copley <zach@controlyourself.ca>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://laconi.ca/
42  *
43  */
44 class TwitterOAuthClient extends OAuthClient
45 {
46     public static $requestTokenURL = 'https://twitter.com/oauth/request_token';
47     public static $authorizeURL    = 'https://twitter.com/oauth/authorize';
48     public static $accessTokenURL  = 'https://twitter.com/oauth/access_token';
49
50     /**
51      * Constructor
52      *
53      * @param string $oauth_token        the user's token
54      * @param string $oauth_token_secret the user's token secret
55      *
56      * @return nothing
57      */
58     function __construct($oauth_token = null, $oauth_token_secret = null)
59     {
60         $consumer_key    = common_config('twitter', 'consumer_key');
61         $consumer_secret = common_config('twitter', 'consumer_secret');
62
63         parent::__construct($consumer_key, $consumer_secret,
64                             $oauth_token, $oauth_token_secret);
65     }
66
67     /**
68      * Builds a link to Twitter's endpoint for authorizing a request token
69      *
70      * @param OAuthToken $request_token token to authorize
71      *
72      * @return the link
73      */
74     function getAuthorizeLink($request_token)
75     {
76         return parent::getAuthorizeLink(self::$authorizeURL,
77                                         $request_token,
78                                         common_local_url('twitterauthorization'));
79     }
80
81     /**
82      * Calls Twitter's /account/verify_credentials API method
83      *
84      * @return mixed the Twitter user
85      */
86     function verifyCredentials()
87     {
88         $url          = 'https://twitter.com/account/verify_credentials.json';
89         $response     = $this->oAuthGet($url);
90         $twitter_user = json_decode($response);
91         return $twitter_user;
92     }
93
94     /**
95      * Calls Twitter's /stutuses/update API method
96      *
97      * @param string $status                text of the status
98      * @param int    $in_reply_to_status_id optional id of the status it's
99      *                                      a reply to
100      *
101      * @return mixed the status
102      */
103     function statusesUpdate($status, $in_reply_to_status_id = null)
104     {
105         $url      = 'https://twitter.com/statuses/update.json';
106         $params   = array('status' => $status,
107             'in_reply_to_status_id' => $in_reply_to_status_id);
108         $response = $this->oAuthPost($url, $params);
109         $status   = json_decode($response);
110         return $status;
111     }
112
113     /**
114      * Calls Twitter's /stutuses/friends_timeline API method
115      *
116      * @param int $since_id show statuses after this id
117      * @param int $max_id   show statuses before this id
118      * @param int $cnt      number of statuses to show
119      * @param int $page     page number
120      *
121      * @return mixed an array of statuses
122      */
123     function statusesFriendsTimeline($since_id = null, $max_id = null,
124                                      $cnt = null, $page = null)
125     {
126
127         $url    = 'https://twitter.com/statuses/friends_timeline.json';
128         $params = array('since_id' => $since_id,
129                         'max_id' => $max_id,
130                         'count' => $cnt,
131                         'page' => $page);
132         $qry    = http_build_query($params);
133
134         if (!empty($qry)) {
135             $url .= "?$qry";
136         }
137
138         $response = $this->oAuthGet($url);
139         $statuses = json_decode($response);
140         return $statuses;
141     }
142
143     /**
144      * Calls Twitter's /stutuses/friends API method
145      *
146      * @param int $id          id of the user whom you wish to see friends of
147      * @param int $user_id     numerical user id
148      * @param int $screen_name screen name
149      * @param int $page        page number
150      *
151      * @return mixed an array of twitter users and their latest status
152      */
153     function statusesFriends($id = null, $user_id = null, $screen_name = null,
154                              $page = null)
155     {
156         $url = "https://twitter.com/statuses/friends.json";
157
158         $params = array('id' => $id,
159                         'user_id' => $user_id,
160                         'screen_name' => $screen_name,
161                         'page' => $page);
162         $qry    = http_build_query($params);
163
164         if (!empty($qry)) {
165             $url .= "?$qry";
166         }
167
168         $response = $this->oAuthGet($url);
169         $friends  = json_decode($response);
170         return $friends;
171     }
172
173     /**
174      * Calls Twitter's /stutuses/friends/ids API method
175      *
176      * @param int $id          id of the user whom you wish to see friends of
177      * @param int $user_id     numerical user id
178      * @param int $screen_name screen name
179      * @param int $page        page number
180      *
181      * @return mixed a list of ids, 100 per page
182      */
183     function friendsIds($id = null, $user_id = null, $screen_name = null,
184                          $page = null)
185     {
186         $url = "https://twitter.com/friends/ids.json";
187
188         $params = array('id' => $id,
189                         'user_id' => $user_id,
190                         'screen_name' => $screen_name,
191                         'page' => $page);
192         $qry    = http_build_query($params);
193
194         if (!empty($qry)) {
195             $url .= "?$qry";
196         }
197
198         $response = $this->oAuthGet($url);
199         $ids      = json_decode($response);
200         return $ids;
201     }
202
203 }