<?php
ini_set('memory_limit', '-1');
// 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);
}