You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In case you want to log out the current user (to log in a different user without
92
+
creating a new Codebird object), just call the `logout()` method.
93
+
94
+
```
95
+
$cb->logout();
96
+
```
97
+
89
98
### Application-only auth
90
99
91
100
Some API methods also support authenticating on a per-application level.
@@ -168,8 +177,9 @@ sent with the code above.
168
177
169
178
### Uploading media to Twitter
170
179
171
-
Tweet media can be uploaded in a 2-step process.
172
-
**First** you send each image to Twitter, like this:
180
+
Tweet media can be uploaded in a 2-step process:
181
+
182
+
**First** you send each media to Twitter. For **images**, it works like this:
173
183
174
184
```php
175
185
// these files to upload. You can also just upload 1 image!
@@ -189,6 +199,8 @@ foreach ($media_files as $file) {
189
199
}
190
200
```
191
201
202
+
Uploading **videos** requires you to send the data in chunks. See the next section on this.
203
+
192
204
**Second,** you attach the collected media ids for all images to your call
193
205
to ```statuses/update```, like this:
194
206
@@ -208,7 +220,7 @@ print_r($reply);
208
220
Here is a [sample tweet](https://twitter.com/LarryMcTweet/status/475276535386365952)
209
221
sent with the code above.
210
222
211
-
More [documentation for tweeting with media](https://dev.twitter.com/rest/public/uploading-media-multiple-photos) is available on the Twitter Developer site.
223
+
More [documentation for uploading media](https://dev.twitter.com/rest/public/uploading-media) is available on the Twitter Developer site.
:warning:*URLs containing Unicode characters should be normalised. A sample normalisation function can be found at http://stackoverflow.com/a/6059053/1816603*
235
+
236
+
#### Video files
237
+
238
+
Uploading videos to Twitter (≤ 15MB, MP4) requires you to send them in chunks.
239
+
You need to perform at least 3 calls to obtain your `media_id` for the video:
240
+
241
+
1. Send an `INIT` event to get a `media_id` draft.
242
+
2. Upload your chunks with `APPEND` events, each one up to 5MB in size.
243
+
3. Send a `FINALIZE` event to convert the draft to a ready-to-tweet `media_id`.
244
+
4. Post your tweet with video attached.
245
+
246
+
Here’s a sample for video uploads:
247
+
248
+
```php
249
+
$file = 'demo-video.mp4';
250
+
$size_bytes = filesize($file);
251
+
$fp = fopen($file, 'r');
252
+
253
+
// INIT the upload
254
+
255
+
$reply = $cb->media_upload(array(
256
+
'command' => 'INIT',
257
+
'media_type' => 'video/mp4',
258
+
'total_bytes' => $size_bytes
259
+
));
260
+
261
+
$media_id = $reply->media_id_string;
262
+
263
+
// APPEND data to the upload
264
+
265
+
$segment_id = 0;
266
+
267
+
while (! feof($fp)) {
268
+
$chunk = fread($fp, 1048576); // 1MB per chunk for this sample
269
+
270
+
$reply = $cb->media_upload(array(
271
+
'command' => 'APPEND',
272
+
'media_id' => $media_id,
273
+
'segment_index' => $segment_id,
274
+
'media' => $chunk
275
+
));
276
+
277
+
$segment_id++;
278
+
}
279
+
280
+
fclose($fp);
281
+
282
+
// FINALIZE the upload
283
+
284
+
$reply = $cb->media_upload(array(
285
+
'command' => 'FINALIZE',
286
+
'media_id' => $media_id
287
+
));
288
+
289
+
var_dump($reply);
290
+
291
+
if ($reply->httpstatus < 200 || $reply->httpstatus > 299) {
292
+
die();
293
+
}
294
+
295
+
// Now use the media_id in a tweet
296
+
$reply = $cb->statuses_update(array(
297
+
'status' => 'Twitter now accepts video uploads.',
298
+
'media_ids' => $media_id
299
+
));
300
+
```
301
+
302
+
:warning: The Twitter API reproducibly rejected some MP4 videos even though they are valid. It’s currently undocumented which video codecs are supported and which are not.
303
+
304
+
:warning: When uploading a video in multiple chunks, you may run into an error `The validation of media ids failed.` even though the `media_id` is correct. This is known. Please check back with this [Twitter community forums thread](https://twittercommunity.com/t/video-uploads-via-rest-api/38177/5).
305
+
306
+
222
307
### Requests with app-only auth
223
308
224
309
To send API requests without an access token for a user (app-only auth),
@@ -250,7 +335,7 @@ map to Codebird function calls. The general rules are:
250
335
Examples:
251
336
-```statuses/show/:id``` maps to ```Codebird::statuses_show_ID('id=12345')```.
0 commit comments