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

Posting Evergreen Content to Twitter automatically at regular intervals from a CSV - Code

Discussion in 'Twitter' started by marketing_hackr, Apr 29, 2017.

  1. marketing_hackr

    marketing_hackr Newbie

    Joined:
    Mar 30, 2017
    Messages:
    3
    Likes Received:
    4
    Gender:
    Male
    Occupation:
    Marketing Consultant, code hacker, caffeine fiend
    Location:
    London
    Home Page:
    First post (yay) so thought I'd make it a goodie.

    As most of you know, “Evergreen content” is owned or earned media which is still relevant and of interest to your target audience weeks, months or even years after publication.

    It's a good idea to use your evergreen content as part of your social broadcasting strategy (consitently sharing value with your connections,

    Paying for services like buffer + hootsuite = silly when you can code... so.

    To this end, I wrote some code to automagically post your shit from a CSV whenever you like. (just add a cron job)

    PHP:
    Code:
    //automatically post from csv to twitter. (c) 2017 marketinghacker.blog | twitter.com/marketinghack3r
    
    $cwd = getcwd();
    $bEnableLogging = true;
    require_once($cwd.'/twitteroauth-master/autoload.php');
    use Abraham\TwitterOAuth\TwitterOAuth;
    global $connection;
    $inputfile = $cwd.'/myevergreencontent.csv';
    $connection = new TwitterOAuth($CONSUMER_KEY="INSERT-CONSUMER-KEY-HERE",
           $CONSUMER_SECRET="INSERT-CONSUMER-SECRET-KEY-HERE",
           $access_token="INSERT-ACCESS-TOKEN-HERE",
           $access_token_secret="INSERT-ACCESS-TOKEN-SERET-HERE");
    shareFromCSV();
    //==================================================================================================================================== 
    function shareFromCSV()
    {
      global $inputfile, $connection;
      $theData = readCSV($inputfile);
      $numrows = sizeof($theData);
      if ($numrows >0)
      {
        $index = rand(0, sizeof($theData)-1);
        $status = $connection->post("statuses/update", ["status" => $theData[$index]['Description'].' '.$theData[$index]['URL']]);
         
        if($bEnableLogging)
        {
          $status_txt='';
          if ($connection->getLastHttpCode() == 200){
            $status_txt= 'Posted tweet id:'.$status->id_str.' at '.$status->created_at.' - text: '.$status->text;
          }
          else
            $status_txt='Error Posting Tweet: '.$theData[$index]['Description'].' '.$theData[$index]['URL'];
          file_put_contents('log.txt', $status_txt.PHP_EOL , FILE_APPEND | LOCK_EX);
        }
      }
    }
    //====================================================================================================================================
    function readCSV($csvFile)
    {
      $aryData = array();
      $header = NULL;
      $handle = fopen($csvFile, "r");
      if($handle)
      {
        while (!feof($handle) )
        {
          $aryCsvData = fgetcsv($handle);
          if(!is_array($aryCsvData))
          {
            continue;
          }
         
          if(is_null($header))
          {
            $header = $aryCsvData;
          }
          elseif (is_array($header) && count($header) == count($aryCsvData))
          {
            $aryData[] = array_combine($header, $aryCsvData);
          }
        }
        fclose($handle);
      }
    
      return $aryData;
    }
    //====================================================================================================================================
     
    
    Full blog post + code: marketinghacker.blog

    If you like it / use it, show some <3 !
     
    • Thanks Thanks x 3
  2. Dark Comedy

    Dark Comedy Junior Member

    Joined:
    Dec 16, 2016
    Messages:
    122
    Likes Received:
    27
    Gender:
    Male
    Just read the post! Extremely awesome!

    For someone that gets overwhelmed with a lot of code and steps.... This looks hard, but it is easy, correct?
     
    • Thanks Thanks x 1
  3. marketing_hackr

    marketing_hackr Newbie

    Joined:
    Mar 30, 2017
    Messages:
    3
    Likes Received:
    4
    Gender:
    Male
    Occupation:
    Marketing Consultant, code hacker, caffeine fiend
    Location:
    London
    Home Page:
    I like to make it as easy as possible, and Fuck paying for all this stuff when you can do it free, right?
     
    • Thanks Thanks x 1