本文介绍了使用 Ruby 进行 Facebook FQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ruby​​ 对 Facebook fql.query 方法执行简单的 GET,但没有成功.

I'm trying to do a simple GET with ruby to the Facebook fql.query method without success.

网址的结构基本上是这样的:

The url is basically structured like this:

https://api.facebook.com/method/fql.query?query=SELECT total_count FROM link_stat WHERE url = "http://twitter.com/"&format=json

我已经在 StackOverflow 上阅读了一些关于如何发出这些请求的帖子,但即使如此,我还是不断收到:

I've read in a few posts here on StackOverflow about how to make those requests, but even tho I keep getting:

/usr/lib/ruby/1.8/net/http.rb:560:in `initialize': getaddrinfo: Name or service not known (SocketError)

/usr/lib/ruby/1.8/net/http.rb:560:in `initialize': getaddrinfo: Name or service not known (SocketError)

在 http_get 函数的第一行.

On the first line of http_get function.

def http_get(domain,path,params)
    return Net::HTTP.get(domain, "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&'))) if not params.nil?
    return Net::HTTP.get(domain, path)
end

def getFacebookStats(url)

    params = {
        :query => 'SELECT total_count FROM link_stat WHERE url = "' + url + '"',
        :format => 'json'
    }

    http = http_get('https://api.facebook.com', '/method/fql.query', params)
    puts http

end

推荐答案

http 调用接受主机,而不是 URL:

The http call accepts a host, not a URL:

def http_get(domain,path,params)
    path = unless params.blank
        path + "?" + params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')
      else
        path
    end
    request = Net::HTTP.get(domain, path)

end

def get_facebook_stats(url)

    params = {
        :query => 'SELECT total_count FROM link_stat WHERE url = "' + url + '"',
        :format => 'json'
    }

    http = http_get('api.facebook.com', '/method/fql.query', params)
    puts http

end

不要在 Ruby 上的方法名称中使用驼峰式大小写.

Please do not use camel case on method names on Ruby.

如果要进行 HTTPS 调用,则必须使用不同的调用:

If you want to make HTTPS calls, you will have to use a different call:

require 'net/http'
require 'net/https'

http = Net::HTTP.new('somehost.com', 443)
http.use_ssl = true
path = '/login.html'

resp, data = http.get(path, nil)

这篇关于使用 Ruby 进行 Facebook FQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 01:14