Skip to main content
Code SamplesPHPSocial Media

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.

[cc lang=”php” lines=”100″]
<?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="".$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:

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!