views:

288

answers:

4

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

+1  A: 

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
chipeau
The issue is I don't have their email address.
tarnfeld
+2  A: 

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.

Bill the Lizard
I once had an interesting online discussion with someone who was complaining that Twitter or some similar service would tell you which of the username or the password you had entered was wrong, which was in his opinion bad security design. This was exactly what I said.
Pascal Cuoq
@Pascal Cuoq: I think it's more of a positive thing that Twitter lets you see what other people are posting without forcing you to sign up yourself.
Bill the Lizard
Great point Pascal!
Lo'oris
@Bill rather than check "Sorry that page doesn't exist" I would check the http response code, that in that case is **404 not-found**
Pablo Fernandez
@Pablo: That will definitely be more reliable. I didn't bother to check and hadn't realized that was a custom 404 page.
Bill the Lizard
Yeah neither did I because it definitely doesn't seem a 404 page, had to firebug it too :)
Pablo Fernandez
+3  A: 

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>
chipeau
A: 

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;
Nicolas