Joey
Joey

Reputation: 1676

What is the format for uploading images using twitteroauth?

Source: https://github.com/abraham/twitteroauth/pull/137

In the above-mentioned link, a Github user, Robhaswell, made an adjustment to Abraham's TwitterOAuth code and added an upload function for uploading images. This is a great addition to the framework, however, there was no proper documentation or example attached to the new code, so I'm having a bit of trouble using the function:

$image = 'weather.jpg';

$response = $tweet->upload('statuses/update_with_media', array(
    'status' => 'This is a test', 
    'media[]' => "@{$image};type=image/jpeg;filename={$image}")
);

Whereas weather.jpg is in the same folder as the file with the code above.

Side note: The code will be executed through cron and upload an image that is always already present on the server, to twitter. Just to clarify that users won't have to be able to upload their images first and then submit them to Twitter via this script.

Can anyone point me in the right direction?

Edit: I'm aware this functionality is not part of the original build and I have updated the twitteroauth and OAuth code in accordance to Robhaswell's adjustment.

Upvotes: 2

Views: 3491

Answers (2)

david2278
david2278

Reputation: 434

Since this question is still relevant, but the answer is now outdated I'll go ahead and update this question with a more up-to-date answer.

statuses/update_with_media has been deprecated by twitter. https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update_with_media.html

Here is my working code that uploads and tweets a status with a picture.

$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret);
$content = $twitter->get("account/verify_credentials");
$tweet = "My tweet";
$imageMedia = $twitter->upload('media/upload', array('media' => '/path/to/image/image_name.png'));
$parameters = array(
    "status" => $tweet,
    "media_ids" => $imageMedia->media_id_string);

$statuses = $twitter->post("statuses/update", $parameters);

I just thought I would post this here since I stumbled across this question while looking for an answer for it.

Upvotes: 5

Arno
Arno

Reputation: 161

Since I've been struggling with this, I thought answering this question might help some other people. This uses abraham's twitteroauth with the added image upload functionality (code can be found here)

You should check if your image-path is correct. It has to point to a file on your server (relative).

Also, make sure the host is correct in the twitteroauth file (see upload function, where it briefly changes the host url, this isn't necessary anymore). You should now use

https://api.twitter.com/1.1/

instead of

https://upload.twitter.com/1/

Keeping these two things in mind following code should work (it does for me);

$this->api = new TwitterOAuth($consumerKey, $consumerSecret,$token, $token_secret);    
$attachment = "./images/img.jpg";
$image = "@{$attachment};type=image/jpeg";
$status = "Text";
$result = $this->api->upload('statuses/update_with_media',array('status'=>$message,'media[]'=>$image));

Upvotes: 3

Related Questions