Twitter API v1.1 – check if username exists

Here is a PHP code snippet to check if username (screen name) exists, by the Twitter API with twitteroauth library by Abraham Williams: https://github.com/abraham/twitteroauth.

Code Snippet – check if username exists

<?php
require_once 'lib/twitteroauth.php';

define('CONSUMER_KEY', 'your_consumer_key');
define('CONSUMER_SECRET', 'your_consumer_secret');
define('ACCESS_TOKEN', 'your_access_token');
define('ACCESS_TOKEN_SECRET', 'your_access_token_secret');

function find_users(array $users)
{
    $founds = array();

    $toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

    // Up to 100 users per request.
    $userAry = array_slice($users, 0, 100);

    // Init with "not found" for all users.
    foreach ($userAry as $user) {
        $founds[$user] = false;
    }

    // Find existing users by "screen_name".
    $userObjs= $toa->post('users/lookup', array('screen_name' => implode(',', $userAry)));

    // Set "found" for existing users.
    foreach ($userObjs as $userObj) {
        $founds[$userObj->screen_name] = true;
    }

    return $founds;
}

The find_users function accepts an array of usernames (screen names) and returns an array of existences with true/false for each given username.

Sample program

$users = array('aaa', 'qwertyuiop0123', '@@@');
$founds = find_users($users);
var_dump($founds);

Output

array(3) {
  ["aaa"]=>
  bool(true)
  ["qwertyuiop0123"]=>
  bool(false)
  ["@@@"]=>
  bool(false)
}

Note

The find_users function checks up to 100 users per call. A single users/lookup request allows up to 100 users (See: https://dev.twitter.com/docs/api/1.1/get/users/lookup).

3 thoughts on “Twitter API v1.1 – check if username exists

  1. Johna963

    I do trust all of the ideas you’ve presented in your post. They’re very convincing and can certainly work. Nonetheless, the posts are very short for beginners. May just you please lengthen them a bit from next time? Thanks for the post. abfdcagecged

  2. MiChAeLoKGB

    Just tried it and its not really working.

    Heres the call:
    $twitter_array = array($twitter, “Jayztwocents”, “Barnacules”, “Barnacules Nerdgasm”, “Inu-ki”, “Inukii”, “HiRezStew”, “schisam”);
    $check_twitter = find_users($twitter_array);

    Theres my result (notice empty string at the end, why its there and why its true?):
    array(9) {
    [“MiChAeLoKGB”]=> bool(false)
    [“Jayztwocents”]=> bool(false)
    [“Barnacules”]=> bool(false)
    [“Barnacules Nerdgasm”]=> bool(false)
    [“Inu-ki”]=> bool(false)
    [“Inukii”]=> bool(false)
    [“HiRezStew”]=> bool(false)
    [“schisam”]=> bool(false)
    [“”]=> bool(true)
    }

    1. techiella Post author

      Your array contains multi-byte double-quote characters. Replace them with single-byte characters, then you can get a correct result:

      $users = array("Jayztwocents", "Barnacules", "Barnacules Nerdgasm", "Inu-ki", "Inukii", "HiRezStew", "schisam");
      $founds = find_users($users);
      var_dump($founds);
      
      array(8) {
        ["Jayztwocents"]=>
        bool(false)
        ["Barnacules"]=>
        bool(true)
        ["Barnacules Nerdgasm"]=>
        bool(false)
        ["Inu-ki"]=>
        bool(false)
        ["Inukii"]=>
        bool(true)
        ["HiRezStew"]=>
        bool(true)
        ["schisam"]=>
        bool(true)
        ["JayzTwoCents"]=>
        bool(true)
      }
      

Comments are closed.