web analytics

logo Meta Data Science

By Massoud Seifi, Ph.D. Data Scientist

How to Get Twitter Username From Twitter ID

Twitter ID and Username

The Twitter ID is a unique number identifying an account on Twitter. Upon registration, users can also choose a username (a.k.a. screen name or @handle). However an account can never change its Twitter ID, but it can change its username.

Getting Twitter username from Twitter ID using the Twitter API (rate limited)

I was trying to lookup some Twitter IDs and find out what their corresponding usernames are. My first approach was using the code below using http://twitter.com/users/show/[ID]?format=json as the endpoint:

Getting Twitter username from Twitter ID using the Twitter API (limited) (TwitterUserIdToScreenName_limited.php) download
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
<?php
/**
 * Getting the Twitter usename from Twitter ID
 * This approach does not work in large scale. It's limited to 150 requests per hour.
 *
 * @author Massoud Seifi, Ph.D. @ MetaDataScience.com
 */

class TwitterUserIdToScreenName {

	protected $href_base = 'http://twitter.com/users/show/';

	public function getScreenNameFromUserId($twitter_user_id){

		if (!is_numeric($twitter_user_id)){
		    return false;
		}

		$href = $this->href_base . $twitter_user_id . '?format=json';

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $href);
		curl_setopt($ch, CURLOPT_HEADER         , false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION , true);

		$result = curl_exec($ch);
		curl_close($ch);

		$json = json_decode($result, true);
		
		if (isset($json['screen_name'])){
			return $json['screen_name'];
		}

	}

}

$p = new TwitterUserIdToScreenName();

while ($line = fgets(STDIN)){
	$id = trim($line);
	$screen_name = $p->getScreenNameFromUserId($id);
	if ( is_string($screen_name) ){
		echo "$id,$screen_name\n";
	}
}

This code calls Twitter API which has two issues:

  1. It needs an access token after the release of Twitter API version 1.1.

  2. The number of requests is limited to 150 requests per hour.

Getting Twitter username from Twitter ID without using the Twitter API (scalable)

I tried another approach using http://twitter.com/account/redirect_by_id?id= which doesn’t call the Twitter API and consequently has no rate limit. This approach worked fine for me and I could easily look up thousands of Twitter IDs. Here is the code:

Getting Twitter username from Twitter ID without using the Twitter API (TwitterUserIdToScreenName.php) download
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
<?php
/**
 * Getting the Twitter usename from Twitter ID
 * This code works fine in large scale.
 *
 * @author Massoud Seifi, Ph.D. @ MetaDataScience.com
 */

class TwitterUserIdToScreenName {

	protected $href_base = 'http://twitter.com/account/redirect_by_id?id=';

	public function getScreenNameFromUserId($twitter_user_id)
	{

		if (!is_numeric($twitter_user_id)){
		    return false;
		}

		$href = $this->href_base . $twitter_user_id;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $href);
		curl_setopt($ch, CURLOPT_NOBODY, true);
		curl_setopt($ch, CURLOPT_HEADER         , true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION , true);

		$result = curl_exec($ch);
		$info =  curl_getinfo($ch);
		curl_close($ch);

		if (isset($info['url'])){
			return  str_replace( '/', '', parse_url($info['url'], PHP_URL_PATH));
		}

	}

}

$p = new TwitterUserIdToScreenName();

while ($line = fgets(STDIN)){
	$id = trim($line);
	$screen_name = $p->getScreenNameFromUserId($id);
	if ( is_string($screen_name) ){
		echo "$id,$screen_name\n";
	}
}

Usage Example:

1
2
> echo 1180576736 | php TwitterUserIdToScreenName.php
1180576736,AccessToken

or you can lookup a list of IDs:

1
> cat list_IDs | php TwitterUserIdToScreenName.php

Comments