Hey guys, I’m trying to use the twitter api to post a status with an attached image. How can I make use of the media_ids property? I don’t understand how I can derive a media_id from an actual image element.
You’ll have to upload it through the media endpoint(s) which will then return an id for you to use in the status.
Would you mind providing a simple example?
It seems to me that uploading the media through the media endpoint would post the photo. I don’t want to have to post twice for one share.
I use this framework: https://github.com/abraham/twitteroauth
Basically it goes:
$request = new \Abraham\TwitterOAuth\TwitterOAuth( 'consumer_key', 'consumer_secret' ); $request->setOauthToken( $post['oauth_token_unserialized']->getToken(), $post['oauth_token_unserialized']->getTokenSecret() ); $media = $request->upload('media/upload', [ 'media' => $attachment['path'] ]); $mediaIds = $media['media_id_str']; $result = $request->post( 'statuses/update', [ 'status' => 'Your message', 'media_ids' => implode(',', $mediaIds) ] );
It won’t post the image twice, it just allows you to upload multiple images as up to 4 images can be used per tweet. You can also attach that media id to more than one tweet.
Also, just for clarity, $request.result.media_id’s would hold the media_ids value that would be used in the tweet?
The result from the $request->upload call contains media_id and media_id_str which can be used in the tweet, correct.
I was trying to accomplish this task by just reading excerpts from the documentation.
I’ll stop being lazy and do my research thoroughly. Thanks for the help!
It’s always good to read.