Get redirect of a URL in Ruby

后端 未结 7 1204
盖世英雄少女心
盖世英雄少女心 2020-12-02 23:18

According to Facebook graph API we can request a user profile picture with this (example):

https://graph.facebook.com/1489686594/picture

Bu

相关标签:
7条回答
  • 2020-12-02 23:37

    You've got HTTPS URLs there, so you will handle that...

    require 'net/http'
    require 'net/https' if RUBY_VERSION < '1.9'
    require 'uri'
    
    u = URI.parse('https://graph.facebook.com/1489686594/picture')
    
    h = Net::HTTP.new u.host, u.port
    h.use_ssl = u.scheme == 'https'
    
    head = h.start do |ua|
      ua.head u.path
    end
    
    puts head['location']
    
    0 讨论(0)
  • 2020-12-02 23:40

    You can check the response status code and get the final URL recursively using something like get_final_redirect_url method:

      require 'net/http'
    
      def get_final_redirect_url(url, limit = 10)
        uri = URI.parse(url)
        response = ::Net::HTTP.get_response(uri)
        if response.class == Net::HTTPOK
          return uri
        else
          redirect_location = response['location']
          location_uri = URI.parse(redirect_location)
          if location_uri.host.nil?
            redirect_location = uri.scheme + '://' + uri.host + redirect_location
          end
          warn "redirected to #{redirect_location}"
          get_final_redirect_url(redirect_location, limit - 1)
        end
      end
    

    I was facing the same issue. I solved it and built a gem final_redirect_url around it, so that everyone can benefit from it.

    You can find the details on uses here.

    0 讨论(0)
  • 2020-12-02 23:42

    Yeah, "Location" response header tell you the actual image URL.

    However, if you use the picture as the user's profile image on your site, I recommend you to use "https://graph.facebook.com/:user_id/picture" style URL instead of actual image URL. Otherwise, your users will see lots of "not found" images, or outdated profile images in the future.

    You just put "https://graph.facebook.com/:user_id/picture" as the "src" attribute of "img" tag. They browser gets the updated image of the user.

    ps. I have such troubles on my site with Twitter & Yahoo! OpenID now..

    0 讨论(0)
  • 2020-12-02 23:47

    You can use Net::Http and read the Location: header from the response

    require 'net/http'
    require 'uri'
    
    url = URI.parse('http://www.example.com/index.html')
    res = Net::HTTP.start(url.host, url.port) {|http|
      http.get('/index.html')
    }
    res['location']
    
    0 讨论(0)
  • 2020-12-02 23:52

    I know this is an old question, but I'll add this answer for posterity:

    Most of the solutions I've seen only follow a single redirect. In my case, I had to follow multiple redirects to get the actual final destination URL. I used Curl (via the Curb gem) like so:

    result = Curl::Easy.perform(url) do |curl|
      curl.head = true
      curl.follow_location = true
    end
    result.last_effective_url
    
    0 讨论(0)
  • 2020-12-02 23:56

    If you want a solution that:

    • does not use gems
    • follows all redirects
    • works also with url-shortening services
    require 'net/http'
    require 'uri'
    
    def follow_redirections(url)   
      response = Net::HTTP.get_response(URI(url))
      until response['location'].nil?
        response = Net::HTTP.get_response(URI(response['location']))
      end
      response.uri.to_s
    end
    
    # EXAMPLE USAGE
    follow_redirections("https://graph.facebook.com/1489686594/picture") 
    # => https://static.xx.fbcdn.net/rsrc.php/v3/yo/r/UlIqmHJn-SK.gif
    
    0 讨论(0)
提交回复
热议问题