Check if twitter username exists

2019-02-04 18:29发布

Is there a way to check if a twitter username exists? Without being authenticated with OAuth or the twitter basic authentication?

9条回答
beautiful°
2楼-- · 2019-02-04 19:18

You can try to grab the http://twitter.com/username page and read the response to see if you get the "Sorry, that page doesn’t exist!" page.

Edit:

As @Pablo Fernandez mentioned in a comment, it will be better (faster, more reliable) to check the response header, which will be "404 not-found" if the user doesn't exist.

查看更多
forever°为你锁心
3楼-- · 2019-02-04 19:24

You can try:

<?php
$user = "toneid";
$contents = @file_get_contents('http://www.twitter.com/'.$user);
if (!$contents) {
    // Report error
    echo "Not a valid user";
    } else {
    // If is a valid url
    echo "OK!";
} 
?>
查看更多
干净又极端
4楼-- · 2019-02-04 19:27

As of right now, you're better off using the API the signup form uses to check username availability in realtime. Requests are of the format:

https://twitter.com/users/username_available?username=whatever

And give you a JSON response with a valid key giving you a true if the username can be registered:

{"valid":false,"reason":"taken","msg":"Username has already been taken","desc":"That username has been taken. Please choose another."}
{"valid":true,"reason":"available","msg":"Available!","desc":"Available!"}
{"valid":false,"reason":"is_banned_word","msg":"Username is unavailable","desc":"The username \"root\" is unavailable. Sorry!"}

The reason this is better than checking for 404 responses is that sometimes words are reserved (like 'root' above), or a username is actually taken but for some reason the account is gone from the Twitter front end.

查看更多
登录 后发表回答