Find Out If A Twitter Username Exists Using PHP/JSON



I’ve been trying to grab a Twitter screenname that people continually register and do not use. Twitter eventually deletes it, but I suppose it is in high enough demand that someone else registers it right away (and then continues to never use it). Wrote up a quick and dirty php script to check the Twitter API to see if a screenname exists and if it doesn’t, shoot a short an email. I’ve been using changedetection to roughly do the same thing but it has fallen short on two counts: the first is that it reports follower count changes, and second it only runs once a day and this hasn’t been fast enough for me to grab my desired screen name.

You can set this script to run via cron at any desired interval, I’ve set it to run every 30 minutes for me. Test with sample information to make sure it works under your setup. I’m using the default mail() function, and I also included some json decoding in there so you could also use this script to play around with decoding a user’s information in PHP.

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
50
51
52
<?php
/* Copyright (c) 2010 Dave Drager
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


/* README
 * This is a simple script to check a username on Twitter and send a short email
 * if it is not found. It's the simplest way I found to do this. Using the
 * sample code you could also modify it simply to do something with a
 * found username as well. Details are in the $json_output array. Enjoy
 */


// fill in these 2 variables below
$username = "someusername";
$notifyemail = "your_email@address";

// this is needed otherwise file_get_contents errors out
error_reporting(0);
$url="http://api.twitter.com/1/users/show.json?screen_name=".$username;
$json = file_get_contents($url,0,null,null);

if($http_response_header[0] == "HTTP/1.1 404 Not Found") {
        //echo "Screen name not taken!\n";
        $to      = $notifyemail;
        $subject = 'Screen name now available - '.$username;
        $message = 'The screen name '.$username.' is now available on Twitter.';
        $headers = 'From: '.$notifyemail . "\r\n";
        mail($to, $subject, $message, $headers, '-f '.$notifyemail);
}
else {
        // screen name exists - you can do something here if you want
        //$json_output = json_decode($json,true);
        //print_r($json_output);
}
?>

If you are running it from cron, add the sample entry:

1
0 * * * * /usr/bin/php /path/to/script.php

The above would run the script every hour on the hour.

Of course you can easily modify this sample code if you are looking to do any kind of similar checking in your web app. If you find this kind of script useful, please let me know!

  • Anonymous

     Great post. I modded this to find my @G64 handle. I thank you in the explanation of my version here: http://goo.gl/9u4dD

  • Great article. Unfortunately the twitter username I want is not being used but it is also not available yet. I guess they hold onto them for sometime and then randomly release them. The problem is that a vacant username and an unused username that is still not available give the same json response. Do you know how one might check if the username is available too? Doesn’t seem like you can create a twitter account or change your username via the API….at least not that I have found

  • Ron D. Lite

    by adding an @ sign in front of any command you can disable the error too.. @file_get_contents

  • Thanks Ron!