Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Post Media / Image to twitter

Tags:

php

twitter

I got code changing twitter status, unfortunately it does not posting image.

Please review my code and help me posting image with text.

 $consumerKey    = '';
 $consumerSecret = '';
 $oAuthToken     = '';
 $oAuthSecret    = '';

include "OAuth.php";
 include "twitteroauth.php";
 require_once('twitteroauth.php');
 $tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken,      $oAuthSecret);
 $tweet->post('statuses/update', array('status' => 'PHP --> example.co.uk <-- ',  'media[]' => "http://www.balanced-hr.com/wp-     content/uploads/2014/08/majster.jpg"));
 echo "your message has been sent to twitter API";
like image 929
Fou Avatar asked Oct 28 '25 18:10

Fou


2 Answers

Seems that you should upload your media first (after creating TwitterOAuth object)

$media = $tweet->upload('media/upload', array('media' => 'path_to_your_file'));

Then set the parameters of your tweet (like status and media)

$parameters = array(
'status' => 'Your Tweet status',
'media_ids' => $media->media_id_string);

You can view the docs to upload up to 4 media resources in the same tweet

And then send the tweet with the parameters.

$send_tweet = $connection->post('statuses/update', $parameters);

You can see an example using media here: https://twitteroauth.com/
And the statuses/update reference here: https://dev.twitter.com/rest/reference/post/statuses/update

like image 185
iJos Avatar answered Oct 31 '25 08:10

iJos


$connection = new TwitterOAuth($consumerkey, $consumersecret, $access_token, $access_token_secret);
$content = $connection->get("account/verify_credentials");

check the connection first

$result = $connection->upload('media/upload', array('media' => 'Relative Path to your media'));

$mediaID = $result->media_id;
$parameters = array('status' => 'First tweet','media_ids' => $mediaID);
$response = $connection->post('statuses/update', $parameters);

The above code worked in my instance

like image 31
Aswathy S Avatar answered Oct 31 '25 09:10

Aswathy S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!