Is there a way to check if a twitter username exists? Without being authenticated with OAuth or the twitter basic authentication?
According to the api docs you can pass an email address to the user/ show method, I would assume that if a user didn't exist you'd get back a 404, which should allow you to determine whether or not the user exists.
eg: http://twitter.com/users/[email protected]
result if not exist :
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<request>/users/[email protected]</request>
<error>Not found</error>
</hash
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.
You can also use the API with username :
eg : http://api.twitter.com/1/users/show.xml?screen_name=tarnfeld
Will give you :
<?xml version="1.0" encoding="UTF-8"?>
<user>
...................
<screen_name>tarnfeld</screen_name>
<location>Portsmouth, UK</location>
.................
</status>
</user>
Or if not exist :
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<request>/1/users/show.xml?screen_name=tarnfeldezf</request>
<error>Not found</error>
</hash>
Here is how it works on PHP :
$user_infos = 'http://api.twitter.com/1/users/show.xml?screen_name='.$username;
if (!@fopen($user_infos, 'r'))
{
return false;
}
return true;