Skip to content

Commit

Permalink
Support uploading multiple media, fix #60
Browse files Browse the repository at this point in the history
  • Loading branch information
mynetx committed Jun 7, 2014
1 parent 17d6599 commit 55854d6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions src/codebird.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ protected function _detectMultipart($method)
$multiparts = array(
// Tweets
'statuses/update_with_media',
'media/upload',

// Users
'account/update_profile_background_image',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 55854d6

Please sign in to comment.