Check if twitter username exists

前端 未结 9 1600
旧巷少年郎
旧巷少年郎 2021-02-05 10:28

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

相关标签:
9条回答
  • 2021-02-05 11:08

    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;
    
    0 讨论(0)
  • 2021-02-05 11:08

    This worked for me, close to what sferik has posted.

    def twitter_user_exists?(user)
       Twitter.user(user)
       true
    rescue Twitter::Error::NotFound
       false
    end
    
    0 讨论(0)
  • 2021-02-05 11:08

    Using Ruby, you could install the twitter gem and then define the following method:

    require 'twitter'
    
    def user_exists?(user)
      Twitter.user(user)
      true
    rescue Twitter::NotFound
      false
    end
    

    Then, simply pass in a Twitter user name or id to your method, like so:

    user_exists?("sferik") #=> true
    user_exists?(7505382) #=> true
    
    0 讨论(0)
提交回复
热议问题