Finding a short Twitter username


A quick and dirty PHP solution

I was looking at available short domain names this weekend, and it got me thinking about short twitter usernames. While my personal twitter handle is only 6 characters long (@rmmoul), I was still curious about how to find available short usernames.

I had a feeling that someone had already tackled this problem, and I was right. I found an article¬†written in 2013 that contained a great write up on the process, along with some PHP and jQuery code to do the search. Unfortunately, the jQuery code constantly caused my browser to crash, and I wasn’t able to get any results. The PHP code that determined if a username was available worked flawlessly, though, so I kept that code and looked for a quick way to generate the possible usernames in PHP.

Over on Stack Overflow, I came across a solution showing how to build the potential usernames using recursion.

I put those two functions together and ended up with the following PHP script. This should be run from the command line to avoid http timeouts since the script will take quite a long time to run. I ran this on my mac, and I did have to increase the CLI PHP memory limit from the 128M default to 512M (not sure if 512 was necessary).

<?php    
    
    // http://forrk.co.uk/blog/Finding-a-short-Twitter-Username-Mission-Impossible/1
    function checkusername($username){
        if(!empty($username) && $username != ''){
            
            $url="http://twitter.com/".$username;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $return = curl_exec($ch);
            $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
                              
            if($http_status == '404'){
                echo 'Available: '.$username."\n";
            }
            else{
                // not available
            } 
        }// if
    }// function checkusername($username)

    // http://stackoverflow.com/a/19067884/901502
    function build_usernames($chars, $size, $combinations = array()){

        // if it's the first iteration, the first set 
        // of combinations is the same as the set of characters
        if(empty($combinations)){
            $combinations = $chars;
        }

        // we're done if we're at size 1
        if ($size == 1) {
            return $combinations;
        }

        // initialise array to put new values in
        $new_combinations = array();

        // loop through existing combinations and character set to create strings
        foreach($combinations as $combination){
            foreach($chars as $char){
                $new_combinations[] = $combination . $char;
            }// foreach
        }// foreach

        // call same function again for the next iteration
        return build_usernames($chars, $size - 1, $new_combinations);

    }// function build_usernames($chars, $size, $combinations = array())

    // set the possible chars to build the username from
    $possible_chars = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm');

    // build the user name combinations for up to 4 char usernames
    $possible_usernames = build_usernames($possible_chars, 4);

    // loop through all usernames and check each with twitter
    foreach($possible_usernames as $username){
        checkusername($username);
    }

As the script runs, you should see output that looks something like this:

Screen Shot 2015-09-19 at 4.38.43 PM