1
|
|
|
<?php |
2
|
|
|
namespace Wheedle; |
3
|
|
|
|
4
|
|
|
use \GuzzleHttp\Exception\ClientException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* A collection of convenience methods for the statuses endpoints for |
8
|
|
|
* the twitter API |
9
|
|
|
* |
10
|
|
|
* @author Matt Frost <[email protected]> |
11
|
|
|
* @package Wheedle |
12
|
|
|
* @license MIT http://opensource.org/licenses/MIT |
13
|
|
|
*/ |
14
|
|
|
class Tweet |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* End point constants |
18
|
|
|
*/ |
19
|
|
|
const USER_TIMELINE_ENDPOINT = 'statuses/user_timeline.json'; |
20
|
|
|
const HOME_TIMELINE_ENDPOINT = 'statuses/home_timeline.json'; |
21
|
|
|
const MENTIONS_ENDPOINT = 'statuses/mentions_timeline.json'; |
22
|
|
|
const MY_RETWEETS_ENDPOINT = 'statuses/retweets_of_me.json'; |
23
|
|
|
const RETRIEVE_ENDPOINT = 'statuses/show/'; |
24
|
|
|
const RETWEETS_ENDPOINT = 'statuses/retweets/'; |
25
|
|
|
const UPDATE_ENDPOINT = 'statuses/update.json'; |
26
|
|
|
const SEND_RETWEET_ENDPOINT = 'statuses/retweet/'; |
27
|
|
|
const DELETE_TWEET_ENDPOINT = 'statuses/destroy/'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Use the options filter trait to eliminate unavailable query string params |
31
|
|
|
*/ |
32
|
|
|
use OptionsFilter; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The Twitter Client for making the requests |
36
|
|
|
* |
37
|
|
|
* @var TwitterClient $client |
38
|
|
|
*/ |
39
|
|
|
private $client; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @param TwitterClient $client |
45
|
|
|
*/ |
46
|
|
|
public function __construct(TwitterClient $client) |
47
|
|
|
{ |
48
|
|
|
$this->client = $client; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Method to retrieve a tweet by id |
53
|
|
|
* |
54
|
|
|
* @param int $id |
55
|
|
|
* @param Array $options optional parameters to refine a search |
56
|
|
|
* - trim_user boolean returns a user object with just numerical ID when true |
57
|
|
|
* - include_my_retweet boolean when true any tweets RT'd by authenticated user |
58
|
|
|
* will have current_user_retweet node |
59
|
|
|
* - include_entites boolean entities node will be excluded when set to false |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function retrieve($id, Array $options = []) |
63
|
|
|
{ |
64
|
|
|
$availableOptions = [ |
65
|
|
|
'trim_user', |
66
|
|
|
'include_my_retweet', |
67
|
|
|
'include_entites' |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
$options = $this->filterOptions($availableOptions, $options); |
71
|
|
|
return $this->client->makeGetRequest(self::RETRIEVE_ENDPOINT . $id .'.json', $options); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Retrieve a collection of mentions for the authenticated user |
76
|
|
|
* |
77
|
|
|
* @param Array $options optional parameters to refine the output |
78
|
|
|
* - count int number of results to return, up to 200, if omitted returns 20 |
79
|
|
|
* - since_id int returns results with an ID more recent than the provided ID |
80
|
|
|
* - max_id int returns results with an ID older than the provided ID |
81
|
|
|
* - trim_user boolean when true returns the user object with only an ID |
82
|
|
|
* - contributor_details boolean when true enhances the contributors element of the response |
83
|
|
|
* - include_entities boolean entities node will be excluded when set to false |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function retrieveMentions(Array $options = []) |
87
|
|
|
{ |
88
|
|
|
$availableOptions = [ |
89
|
|
|
'contributor_details', |
90
|
|
|
'count', |
91
|
|
|
'include_entites', |
92
|
|
|
'max_id', |
93
|
|
|
'since_id', |
94
|
|
|
'trim_user' |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
$options = $this->filterOptions($availableOptions, $options); |
98
|
|
|
return $this->client->makeGetRequest(self::MENTIONS_ENDPOINT, $options); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* A method to return the tweets in a users timeline |
103
|
|
|
* |
104
|
|
|
* @param Array $options optional parameters to refine a search |
105
|
|
|
* - user_id int user id for whom to return results for (if blank defaults to authenticated user) |
106
|
|
|
* - screen_name string screen name for whom to return results for (if blank, defaults to authenticated user) |
107
|
|
|
* - since_id int returns results with an ID more recent than the provided ID |
108
|
|
|
* - count int number of results to return, up to 200 |
109
|
|
|
* - max_id int returns results with an ID older than the provided ID |
110
|
|
|
* - trim_user boolean when true returns the user object with only an ID |
111
|
|
|
* - exclude_replies boolean when true, prevents replies from appearing in the returned timeline |
112
|
|
|
* - contributor_details boolean when true enhances the contributors element of the response |
113
|
|
|
* - include_rts boolean when false the timeline will strip any native retweets |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function retrieveUserTimeline(Array $options = []) |
117
|
|
|
{ |
118
|
|
|
$availableOptions = [ |
119
|
|
|
'user_id', |
120
|
|
|
'screen_name', |
121
|
|
|
'since_id', |
122
|
|
|
'count', |
123
|
|
|
'max_id', |
124
|
|
|
'trim_user', |
125
|
|
|
'exclude_replies', |
126
|
|
|
'contributor_details', |
127
|
|
|
'include_rts' |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
// filter out options that aren't available |
131
|
|
|
$options = $this->filterOptions($availableOptions, $options); |
132
|
|
|
return $this->client->makeGetRequest(self::USER_TIMELINE_ENDPOINT, $options); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Method to retrieve the home timeline for the authenticated user |
137
|
|
|
* |
138
|
|
|
* @param Array $options |
139
|
|
|
* - count int number of results to return, up to 200, if omitted returns 20 |
140
|
|
|
* - since_id int returns results with an ID more recent than the provided ID |
141
|
|
|
* - max_id int returns results with an ID older than the provided ID |
142
|
|
|
* - include_entities boolean entities node will be excluded when set to false |
143
|
|
|
* - exclude_replies boolean when true, prevents replies from appearing in the returned timeline |
144
|
|
|
* - contributor_details boolean when true enhances the contributors element of the response |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function retrieveHomeTimeline(Array $options = []) |
148
|
|
|
{ |
149
|
|
|
$availableOptions = [ |
150
|
|
|
'count', |
151
|
|
|
'since_id', |
152
|
|
|
'max_id', |
153
|
|
|
'include_entities', |
154
|
|
|
'exclude_replies', |
155
|
|
|
'contributor_details' |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
$options = $this->filterOptions($availableOptions, $options); |
159
|
|
|
return $this->client->makeGetRequest(self::HOME_TIMELINE_ENDPOINT, $options); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Retrieving a collection of your tweets that were retweeted by others |
164
|
|
|
* |
165
|
|
|
* @param Array $options |
166
|
|
|
* - count int number of results to return, up to 200, if omitted returns 20 |
167
|
|
|
* - since_id int returns results with an ID more recent than the provided ID |
168
|
|
|
* - max_id int returns results with an ID older than the provided ID |
169
|
|
|
* - trim_user boolean when true returns the user object with only an ID |
170
|
|
|
* - include_entities boolean tweet entities node will be excluded when set to false |
171
|
|
|
* - include_user_entities boolean user entities node will be excluded when set to false |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function retrieveMyRetweets(Array $options = []) |
175
|
|
|
{ |
176
|
|
|
$availableOptions = [ |
177
|
|
|
'count', |
178
|
|
|
'include_entities', |
179
|
|
|
'include_user_entities', |
180
|
|
|
'max_id', |
181
|
|
|
'since_id', |
182
|
|
|
'trim_user', |
183
|
|
|
]; |
184
|
|
|
|
185
|
|
|
$options = $this->filterOptions($availableOptions, $options); |
186
|
|
|
return $this->client->makeGetRequest(self::MY_RETWEETS_ENDPOINT, $options); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Retrieve the retweets for a specific tweet |
191
|
|
|
* |
192
|
|
|
* @param int $id ID of the tweet to retrieve the retweeters |
193
|
|
|
* @param Array $options |
194
|
|
|
* - count int number of results to return up to 100, 100 is the max allowed |
195
|
|
|
* - trim_user boolean when true returns the user object with only an ID |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function retrieveRetweets($id, $options) |
199
|
|
|
{ |
200
|
|
|
$availableOptions = [ |
201
|
|
|
'count', |
202
|
|
|
'trim_user' |
203
|
|
|
]; |
204
|
|
|
|
205
|
|
|
$options = $this->filterOptions($availableOptions, $options); |
206
|
|
|
return $this->client->makeGetRequest(self::RETWEETS_ENDPOINT . $id . '.json', $options); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Method to create a Tweet |
211
|
|
|
* |
212
|
|
|
* @param string $status The status or tweet that will be sent |
213
|
|
|
* @param Array $options Optional parameters for the request |
214
|
|
|
* - in_reply_to_status_id int the ID for the tweet you are replying to, must @ mention original author |
215
|
|
|
* - possibly_sensitive boolean should be set when tweet contains nudity, violence or other gross stuff |
216
|
|
|
* - lat float latitude |
217
|
|
|
* - long float longitude |
218
|
|
|
* - place_id string a place in the world |
219
|
|
|
* - display_coordinates boolean when true will put a pin an exact coordinates tweet was sent from |
220
|
|
|
* - trim_user boolean when true returns the user object with only an ID |
221
|
|
|
* - media_ids string a list of media ids to associate to a tweet |
222
|
|
|
*/ |
223
|
|
|
public function create($status, $options = []) |
224
|
|
|
{ |
225
|
|
|
$availableOptions = [ |
226
|
|
|
'in_reply_to_status_id', |
227
|
|
|
'possibly_sensitive', |
228
|
|
|
'lat', |
229
|
|
|
'long', |
230
|
|
|
'place_id', |
231
|
|
|
'display_coordinates', |
232
|
|
|
'trim_user', |
233
|
|
|
'media_ids' |
234
|
|
|
]; |
235
|
|
|
|
236
|
|
|
$options = $this->filterOptions($availableOptions, $options); |
237
|
|
|
$options['status'] = $status; |
238
|
|
|
ksort($options); |
239
|
|
|
return $this->client->makePostRequest(self::UPDATE_ENDPOINT, $options); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Method to retweet an existing tweet |
244
|
|
|
* |
245
|
|
|
* @param int $id Id for a specific tweet to be retweeted |
246
|
|
|
* @param Array $options Optional parameters for the request |
247
|
|
|
* - trim_user boolean when true returns the user object with only an ID |
248
|
|
|
*/ |
249
|
|
|
public function retweet($id, $options = []) |
250
|
|
|
{ |
251
|
|
|
$availableOptions = ['trim_user']; |
252
|
|
|
$options = $this->filterOptions($availableOptions, $options); |
253
|
|
|
return $this->client->makePostRequest(self::SEND_RETWEET_ENDPOINT . $id . '.json', $options); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Method for deleting a tweet that you've previously published |
258
|
|
|
* |
259
|
|
|
* @param int $id id for the tweet to be deleted |
260
|
|
|
* @param Array $options Optional parameters for the request |
261
|
|
|
* - trim_user boolean when true returns the user object with only an ID |
262
|
|
|
*/ |
263
|
|
|
public function delete($id, $options = []) |
264
|
|
|
{ |
265
|
|
|
$availableOptions = ['trim_user']; |
266
|
|
|
$options = $this->filterOptions($availableOptions, $options); |
267
|
|
|
return $this->client->makePostRequest(self::DELETE_TWEET_ENDPOINT . $id . '.json', $options); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|