10000 Update README with video upload instructions · balitax/codebird-php@21bfc4e · GitHub
[go: up one dir, main page]

Skip to content

Commit 21bfc4e

Browse files
committed
Update README with video upload instructions
1 parent b3a669d commit 21bfc4e

File tree

1 file changed

+78
-3
lines changed

1 file changed

+78
-3
lines changed

README.md

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ sent with the code above.
177177

178178
### Uploading media to Twitter
179179

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:
182183

183184
```php
184185
// these files to upload. You can also just upload 1 image!
@@ -198,6 +199,8 @@ foreach ($media_files as $file) {
198199
}
199200
```
200201

202+
Uploading **videos** requires you to send the data in chunks. See the next section on this.
203+
201204
**Second,** you attach the collected media ids for all images to your call
202205
to ```statuses/update```, like this:
203206

@@ -217,7 +220,7 @@ print_r($reply);
217220
Here is a [sample tweet](https://twitter.com/LarryMcTweet/status/475276535386365952)
218221
sent with the code above.
219222

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.
221224

222225
#### Remote files
223226

@@ -230,6 +233,78 @@ $reply = $cb->media_upload(array(
230233

231234
:warning: *URLs containing Unicode characters should be normalised. A sample normalisation function can be found at http://stackoverflow.com/a/6059053/1816603*
232235

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+
233308
### Requests with app-only auth
234309

235310
To send API requests without an access token for a user (app-only auth),

0 commit comments

Comments
 (0)
0