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
Copy file name to clipboardExpand all lines: README.md
+78-3Lines changed: 78 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -177,8 +177,9 @@ sent with the code above.
177
177
178
178
### Uploading media to Twitter
179
179
180
-
Tweet media can be uploaded in a 2-step process.
181
-
**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:
182
183
183
184
```php
184
185
// these files to upload. You can also just upload 1 image!
@@ -198,6 +199,8 @@ foreach ($media_files as $file) {
198
199
}
199
200
```
200
201
202
+
Uploading **videos** requires you to send the data in chunks. See the next section on this.
203
+
201
204
**Second,** you attach the collected media ids for all images to your call
202
205
to ```statuses/update```, like this:
203
206
@@ -217,7 +220,7 @@ print_r($reply);
217
220
Here is a [sample tweet](https://twitter.com/LarryMcTweet/status/475276535386365952)
218
221
sent with the code above.
219
222
220
-
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*
232
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([
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([
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([
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([
297
+
'status' => 'Twitter now accepts video uploads.',
298
+
'media_ids' => $media_id
299
+
]);
300
+
301
+
```
302
+
303
+
: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.
304
+
305
+
: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).
306
+
307
+
233
308
### Requests with app-only auth
234
309
235
310
To send API requests without an access token for a user (app-only auth),
0 commit comments