1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Script to automate upload and posting of Tweet+Image

Discussion in 'Twitter' started by davids355, Sep 30, 2015.

  1. davids355

    davids355 Jr. VIP Jr. VIP

    Joined:
    Apr 25, 2011
    Messages:
    10,198
    Likes Received:
    7,849
    Home Page:
    This is a very simple tool that I built to post images and text to twitter - fully automated.
    Pretty basic but just thought some people might find it useful.
    Credit to this site for most of the work (Twitter authentication etc):
    https://github.com/dg/twitter-php

    NOTE: I used the tool to upload images for adult twitter account, for me I wanted a tool so I could have a set of folders each containing images of particular models and then pick a model at random, pick an image at random, upload and tweet it and also add a personalized message for that particular model - in this case I wanted to include the models twitter handle which encourages retweets from them.
    this could be modified to different needs or that part could be removed and it could just be used to upload random image from a big list of images.

    NOTE2: All you need for this to work is a hosting account that has FTP access and ability to add cronjobs - most accounts with cpanel will do this. You also need a twitter account of course.

    You need Twitter PHP oauth files from here to make this work:
    https://github.com/dg/twitter-php/tree/master/src
    Download the 3 files from this page and put them in same directory with the below script.

    Folder structure is like this:

    send.php (The file below)
    OAuth.php
    twitter.class.php
    Twitter.php
    girls (Folder)
    -model a
    --img1.jpg
    --img2.jpg
    --etc.jpg
    -model b
    --img1.jpg
    --img2.jpg
    --etc.jpg
    -etc

    You can change folder structure from within script below if needed.

    Then you need to make an application from your twitter account - just log in and then click on "Developers" then click "manage your apps".
    Click on Create new app.
    Enter website details etc and for callback URL it can just be any page on your site where user will be returned after success.
    Once the app is created in permissions make sure it has read and write access and also that it has "allow this application to sign in to twitter" ticked in settings.
    Make a note of consumer key, consumer secret, access token and access token secret so you can enter them in the below script.

    To automate everything you can just add a cronjob that runs the PHP file on a regular basis (Make sure you give the php file execute permission as well - this can be done via FTP or shell.

    Cronjob looks something like this (path to PHP followed by full path to the below file):

    /usr/bin/php /home/account-name/public_html/twitter/send.php

    I used this script to build up 20k+ followers so hopefully it can be of use to someone else as well.

    Notes re script:
    Basically the script picks a random directory under the girls folder, then it stores the name of that directory, then it picks a random image inside that directory, then it composes a tweet based on the name of the directory it chose, uploads the image to twitter and posts the tweet, then it deletes the image from the folder to avoid posting the same image again.

    Code:
    <?php
    
    //this gets a random directory
    
    function get_random_dir($base_dir) 
    { 
      $dirs = glob(sprintf('%s/*',$base_dir),GLOB_ONLYDIR); 
    
      return empty($dirs) ? null : $dirs[array_rand($dirs)]; 
    } 
    
    //set max execution time in case we used all images
    
    ini_set("max_execution_time", "1"); 
    
     while(empty($img)) {
    
    //my directory is called girls and it is in same folder as this php script
    //you can change name of directory to wahtever you want
    //I have a list of directories inside girls, each one is named the name of a model eg - cindy crawford
    
    //get random folder name from inside girls directory 
    $girlsname=get_random_dir('girls');
    
    //reformat the directory path using the chosen subfolder
    $girlsname=str_replace('girls/', '', $girlsname);
    
    //creates structure for folder path
    
    $imagesDir = 'girls/'.$girlsname.'/';
    //gets a random file from that directory and ads to variable
    
    $images = glob($imagesDir . '*.{jpg,jpeg}', GLOB_BRACE);
    $file = $images[array_rand($images)]; // See comments
    $img=$file;
    
    }
    
    //Here I am just assigning text to a variable (girlsname) based on 
    //the name of the random directory that I chose
    
    if ($girlsname=='model a'){$girlsname='please retweet @model-a-twitter-handle';}
    elseif ($girlsname=='model b'){$girlsname='please retweet @model-b-twitter-handle';}
    
    else {$girlsname='generic tweet if we dont find a match';}
    
    $txt='tweet, including the tailored message '.$girlsname;
    
    require_once 'twitter.class.php';
    
    // ENTER HERE YOUR CREDENTIALS (see readme.txt)
    
    $consumerKey='';
    $consumerSecret='';
    $accessToken='';
    $accessTokenSecret='';
    
    $twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
    
    try {
        $tweet = $twitter->send($txt, $img); // you can add $imagePath as second argument
    
    } catch (TwitterException $e) {
        echo 'Error: ' . $e->getMessage();
    }
    
    //once we have used the image we delete it to avoid repetition
    unlink($img);
    
    ?>
    
     
    • Thanks Thanks x 4
  2. davids355

    davids355 Jr. VIP Jr. VIP

    Joined:
    Apr 25, 2011
    Messages:
    10,198
    Likes Received:
    7,849
    Home Page:
    Bumping in case anyone interested in this script :-/
     
  3. zenithpk

    zenithpk Newbie

    Joined:
    Sep 25, 2015
    Messages:
    14
    Likes Received:
    3
    Nice, downloading :)
     
    • Thanks Thanks x 1
  4. SocialMediaPromos

    SocialMediaPromos Newbie

    Joined:
    Mar 22, 2015
    Messages:
    20
    Likes Received:
    1
    Does this let you post words with your image?
     
  5. davids355

    davids355 Jr. VIP Jr. VIP

    Joined:
    Apr 25, 2011
    Messages:
    10,198
    Likes Received:
    7,849
    Home Page:
    ^u yes you can. When I used it I also had an if statement so that I could post text relevant to the folder name. This allowed for personalised tweets which encourages more response. That is in the code in the OP.