Posting Evergreen content to Twitter from a CSV file or Google Sheet

Learn how you can boost your content marketing efforts by automatically sharing posts to Twitter from a CSV or Google Spreadsheet at regular intervals.

What is ‘Evergreen Content’ and Why should I care?

“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.

Why you should be using Evergreen content broadcasts as part of your content-marketing and social-media strategy

The value of evergreen content is that it can continue to deliver traffic, leads, conversions and social shares and can occupy valuable positions in search-engine results pages for a prolonged period of time after initial creation & publication.

Using evergreen should can be a valuable part of a comprehensive content marketing strategy, and it becomes even more valuable when combined with SEO techniques. (Consider basing your evergreen pieces around keywords you’d like your site to rank for)

How can i set up an evergreen content broadcasting system to share content from a spreadsheet or CSV to Twitter?

Prerequisites

Setting things up is easy, but you before you start you need

  • A Twitter account (phone verified)
  • A web-server (where your website is hosted) which supports PHP (if your site is built on WordPress you’re running PHP)
  • The Ability to crete CRON jobs (usually this is configured accessed via your website hosting Control Panel) to set up scheduling. (although If you’re running WordPress as your content management system, there’s a non-cron workaround you can use)

Creating an CSV to Twitter autoposter application in php

  1. Firstly you need to create what is known as ‘a Twitter app’, which will let you (or some code) post to your Twitter account using The Twitter REST API. Once your Twitter app is configured and created, you will receive four important pieces of data:
    • consumer_key
    • consumer_secret
    • access_token
    • access_token_secret

    Read our guide to setting up a Twitter app.

  2. Create a CSV file named ‘myevergreencontent.csv’ with the following headings:
    URL,Description

    Now add some data rows (Tweet text and links to posts you want to automatically broadcast on Twitter), as well as any required URL tracking parameters

  3. Download the PHP TwitterAuth API by Abraham.
    We will use this fabulous library to get PHP code to interact with the Twitter API (once you have supplied it with your access tokens from your Twitter APP.)
  4. Connect to your web-server via FTP and create a folder in the web-root for this project (e.g. public_html/evergreen-scheduler/).
    Once you have created the folder, decompress and extract the twitteroauth-master directory (downloaded in the last step) into the ‘evergreen-scheduler’ directory and also upload your myevergreencontent.csv there too.
    Test that everything is in order by visiting yoursite.com/evergreen-scheduler/myevergreencontent.csv
    If you can see your CSV data when you visit this URL, you have placed the file in the correct server location.
  5. Now you need to create an index.php file (this will use the API and your credentials, and the CSV file to perform the broadcast from your Twitter Account.
    Save the following code to /evergreen-scheduler/index.php – inserting your own access credentials where instructed to do so in the code.

    
    	//Post to Twitter from a Local CSV File at specific time intervals
    	//(c) 2017 -  https://Marketinghacker.blog   - Twitter @marketinghack3r
    	//file: index.php
    
    	$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;
    	}
    	//====================================================================================================================================
    	//Via https://Marketinghacker.blog  
    
  6. Testing time: Visit yoursite.com/evergreen-scheduler/index.php to trigger your new script. (You should just see a blank page). Then visit your Twitter page. You should now see a fabulous new Tweet on your Twitter timleine, posted automatically via PHP to Twitter using the Twitter REST API to post (a random tweet) from your CSV file.
  7. The final step is to set up a CRON job to trigger a call to index.php at a specific interval. (If you’re already familiar with cron, great, you know what to do. If not:) you need to craft a ‘cron expression’ string to (a data string to tell the your server which script to call, and when to do it), and you need to save this in the Cron settings on your webserver (usually done via your hosting control panel).

    The Cronmaker is a useful tool for generating the correct ‘cron Expression’ string.
    Visiting the cronmaker site, we selected ‘every weekday’ at 11:00 and we clicked ‘Generate Cron Expression’.
    Resulting in the following output:

    0 0 11 1/1 * ? *

    In your hosting Control panel find the CRON settings and add a new entry like this (replacing the part before the ‘wget’ below with the cron string you created on cronmaker, and your own domain name)

    0 0 11 1/1 * ? * wget -O - https://yoursite.com/evergreen-scheduler/index.php >/dev/null 2>&1

    (Tech Note: Using -O means that the output of the web request will be sent to STDOUT (standard output), by adding >/dev/null we instruct standard output to be redirect to a black hole. by adding 2>&1 we instruct STDERR (errors) to also be sent to STDOUT, and thus all output will be sent to a blackhole. (so it will load the website, but never write a file anywhere).

Congratulations – you have just created a Twitter APP, built a 100% free Evergreen content broadcast tool for Twitter, and mastered the art of the CRON job.

Automation is awesome. evergreen content is awesome, and YOU are awesome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Marketing Hacker

Online Marketing Tips, Hacks & Code Snippets