From 55854d6d0c1cd4ed5020965c59709853e12d7912 Mon Sep 17 00:00:00 2001 From: "J.M" Date: Sat, 7 Jun 2014 16:31:46 +0200 Subject: [PATCH] Support uploading multiple media, fix #60 --- README.md | 42 +++++++++++++++++++++++++++++++++++++++++- src/codebird.php | 2 ++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b75d4f7..a0f943b 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,9 @@ $params = array( $reply = $cb->users_show($params); ``` -When **uploading files to Twitter**, the array syntax is obligatory: +### Uploading files to Twitter + +The array syntax is obligatory: ```php $params = array( @@ -155,6 +157,44 @@ $params = array( $reply = $cb->statuses_updateWithMedia($params); ``` +#### Multiple images +can be uploaded in a 2-step process. **First** you send each image to Twitter, like this: +```php +// these files to upload +$media_files = array( + 'bird1.jpg', 'bird2.jpg', 'bird3.jpg' +); +// will hold the uploaded IDs +$media_ids = array(); + +foreach ($media_files as $file) { + // upload all media files + $reply = $cb->media_upload(array( + 'media' => $file + )); + // and collect their IDs + $media_ids[] = $reply->media_id_string; +} +``` +**Second,** you attach the collected media ids for all images to your call +to ```statuses/update```, like this: + +```php +// convert media ids to string list +$media_ids = implode(',', $media_ids); + +// send tweet with these medias +$reply = $cb->statuses_update(array( + 'status' => 'These are some of my relatives.', + 'media_ids' => $media_ids +)); +print_r($reply); +); +``` +Here is a [sample tweet](https://twitter.com/LarryMcTweet/status/475276535386365952) sent with the code above. + +More [documentation for tweeting with multiple media](https://dev.twitter.com/docs/api/multiple-media-extended-entities) is available on the Twitter Developer site. + ### Requests with app-only auth To send API requests without an access token for a user (app-only auth), diff --git a/src/codebird.php b/src/codebird.php index 571e65b..b5cd823 100644 --- a/src/codebird.php +++ b/src/codebird.php @@ -833,6 +833,7 @@ protected function _detectMultipart($method) $multiparts = array( // Tweets 'statuses/update_with_media', + 'media/upload', // Users 'account/update_profile_background_image', @@ -862,6 +863,7 @@ protected function _buildMultipart($method, $params) $possible_files = array( // Tweets 'statuses/update_with_media' => 'media[]', + 'media/upload' => 'media', // Accounts 'account/update_profile_background_image' => 'image', 'account/update_profile_image' => 'image',