Check if given user exists on Facebook
How to check if some user exists on Facebook?

My solution was trying to access `http://www.facebook.com/USER` and check the http headers (200 = OK; 404 = NOT FOUND). I could use this code:

    require 'open-uri'
    require 'net/http'
    
    def remote_file_exists?(url,httpcode)
      url = URI.parse(url)
      Net::HTTP.start(url.host, url.port) do |http|
        return http.head(url.request_uri).code == httpcode
      end
    end

The problem is that Facebook always returns 302 (Found), then redirects to `https://www.facebook.com/USER`.

I can require `net/https` and create a new function:


    def https_url_exists? (url,httpcode)
      url = URI.parse(url)
      net = Net::HTTP.new(url.host, url.port) 
      net.use_ssl = true
      net.verify_mode = OpenSSL::SSL::VERIFY_NONE
      net.start do |http|
        return (http.head(url.request_uri).code == httpcode)
      end
    end

Now the problem is that some users use dots on their usernames. For example, username might be user.name. Facebook use redirections for this.

**What's the best way to check if USERNAME exists on facebook? How to get USER.NAME if USERNAME redirects to it?**
Check if given user exists on Facebook
===

How to check if some user exists on Facebook?

My solution was trying to access `http://www.facebook.com/USER` and check the http headers (200 = OK; 404 = NOT FOUND). I could use this code:

    require 'open-uri'
    require 'net/http'
    
    def remote_file_exists?(url,httpcode)
      url = URI.parse(url)
      Net::HTTP.start(url.host, url.port) do |http|
        return http.head(url.request_uri).code == httpcode
      end
    end

The problem is that Facebook always returns 302 (Found), then redirects to `https://www.facebook.com/USER`.

I can require `net/https` and create a new function:


    def https_url_exists? (url,httpcode)
      url = URI.parse(url)
      net = Net::HTTP.new(url.host, url.port) 
      net.use_ssl = true
      net.verify_mode = OpenSSL::SSL::VERIFY_NONE
      net.start do |http|
        return (http.head(url.request_uri).code == httpcode)
      end
    end

Now the problem is that some users use dots on their usernames. For example, username might be user.name. Facebook use redirections for this.

**What's the best way to check if USERNAME exists on facebook? How to get USER.NAME if USERNAME redirects to it?**