Creating lists in twitter is a fantastic way to curate tweets for your perusal based on the interests you have. It would be just as simple to follow any and all accounts you enjoy based on all of your interests but the problem here is, you probably have a lot of interests. Once you follow all the accounts related to all of your interests, you can no longer manage your own timeline. Oddly, as the number of accounts you follow grows, the engagement value of your own timeline declines. This is where lists come in. We can create lists based on our interests to follow many accounts, while still keeping our timeline at a nice level. Let’s tackle creating lists with the Twitter API right now!
Why Use Twitter the API?
As you can see, there is a lot of discussion about programming here at Vegibit. So the first answer to this question is simple. It’s Fun! Beyond that, creating lists can be a time consuming activity. For this example, we’ll create a list, find 20 of the top twitter accounts for our chosen interest, then add all of those users to our new list with one simple PHP script!
1. Create a Twitter App
In order to complete this exercise, you need a twitter app from https://dev.twitter.com/. It’s very easy to set up and once complete you can use your app key
, your app secret
, your access token
, and your access token secret
in the script.
2. Create the PHP script
For this example we create a Codeigniter Controller called Listr with a create method to complete our task.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php class Listr extends CI_Controller { private $_consumer_key = 'your app key'; private $_consumer_secret = 'your app secret'; public function __construct() { parent::__construct(); // https://raw.github.com/abraham/twitteroauth/master/twitteroauth/twitteroauth.php $this->load->library('twitter'); } public function create() { $interest = $this->uri->segment(4); // grab the interest // authenticate to twitter api $this->twitter->TwitterOAuth($this->_consumer_key, $this->_consumer_secret, 'your access token', 'your access token secret'); // create a list giving it a name and description based on our interest $this->twitter->post("lists/create", array( "name" => $interest, "description" => "A custom list about $interest" )); // search for twitter recommended users about our interest $users = $this->twitter->get("users/search", array( "q" => $interest, "count" => '20' )); // extract all screen names and assemble them into one big string // separated by commas foreach($users as $user) { $string .= $user->screen_name . ','; } // take the last comma off the end of said string $string = rtrim($string, ','); // add all of our chosen screen names to our newly created list $users = $this->twitter->post("lists/members/create_all", array( "slug" => $interest, "owner_screen_name" => 'vegibit', "screen_name" => $string )); } } ?> |
3. Run the PHP script
We run the script by loading up the url in our browser. For a localhost installation such as the one used for this example, it would be something like http://localhost/debug/listr/create/marketing
. Let’s test it out now and see what happens!
And… Bazinga!! Here we can see simply by loading our small program in our browser, a fresh new list called ‘marketing’ is created with 20 awesome twitter accounts added, in about 3 seconds!
Thank you for reading 3 Easy Steps to Creating Awesome Twitter lists via the Twitter API – If you found this post helpful, Please do share using the buttons below!