Jump to content

How to post Tweets to Twitter API with TwitterOauth


EyeDentify
 Share

Recommended Posts

Hello There Fellow PW lovers.

I wanted to share this code that i created to post tweets to the Twitter API in an upcomming project i am working on.

And i Thought that everyone could have some use for it.

I am using Composer to load the TwitterOAuth PHP library for working with the Twitter API in this code example.

Also you need to create an App and have your own API keys ready, you can do that here:
https://developer.twitter.com/en/apps

The code posts a string and uploads media to be used with the tweet.
For the TwitterOAuth methods information see the link above.

Note: The paths to the media needs to be local for TwitterOAuth to pick them up and upload them.
See this link for filesize limits and other info on media attachements.

https://developer.twitter.com/en/docs/media/upload-media/overview

<?PHP
/*
    TwitterOAuth API via Composer
    https://twitteroauth.com/
*/
include('vendor/autoload.php');

/* define the classes */
use Abraham\TwitterOAuth\TwitterOAuth;

function postTweetUpdate($str = '', $mediaArray = null) {

    /*
        Twitter OAuth keys
        Create your App and find your API keys here:
        https://developer.twitter.com/en/apps
    */
    $consumer_key = '';
    $consumer_secret = '';
    $access_token = '';
    $access_token_secret = '';
    
    /* init API */
    $connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
    
    if(is_array($mediaArray)) {
        
        $mediaIDS = array();
        
        /*
            Up to 4 media objects can be uploaded.
            https://developer.twitter.com/en/docs/media/upload-media/overview
        */
        
        foreach($mediaArray AS $key => $media_path) {
            
            /* Upload media to twitter API and get media ID back */
            $mediaOBJ = $connection->upload('media/upload', ['media' => $media_path]);
            
            /* push uploaded media ID to array */
            array_push($mediaIDS, $mediaOBJ->media_id_string);
        }
        
        /* create comma delimited list of media ID:s */
        $mediaIDstr = implode(',', $mediaIDS);
    }
        
    /* API params */
    $arrayCfg['status'] = $str;
    $arrayCfg['media_ids'] = $mediaIDstr;
    
    /*
        Make POST request to Twitter API
        https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update
    */
    $statuses = $connection->post("statuses/update", $arrayCfg);
    
    /* return payload */
    return $statuses;
}

?>
<?PHP
    /* string to Post */
    $str = 'Test Tweet... ' . PHP_EOL;
    $str .= '#mytweet' . PHP_EOL;
    $str .= 'http://www.mywebsite.com' . PHP_EOL;
    $str .= '' . hash('sha1', mt_rand(0, 1000000));
    
    /* media to upload */
    $mediaToUpload[] = 'images/my_image.jpg';

    /* make request to Twitter API */
    $payLoad = postTweetUpdate($str, $mediaToUpload);
?>

<h3>Debug</h3>
<pre><?PHP print_r($payLoad); ?></pre>

I have the Debug part because its handy to see what is returned from the Twitter API.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...